实体框架代码首先生成数据库不正确

时间:2013-05-01 03:09:48

标签: asp.net-mvc-4 ef-code-first one-to-many

我认为在我的项目中生成数据库时实体框架存在问题。奇怪的是,它只发生在一个案例中。这是“用户”和“播放列表”之间的一对多关系。一个用户有很多播放列表。

这是我的代码,我在项目中使用了一些抽象类。 核心代码

播放列表

public virtual User User { get; set; }

用户类

public virtual ICollection<Playlist> Playlists { get; set; }

完整代码:

通用类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace xxx.Areas.admin.Models
{
    public abstract class Generic
    {
        [Display(Name = "Ngày tạo")]
        public DateTime? Created { get; set; }
        [Display(Name = "Lần sửa cuối")]
        public DateTime? Modified { get; set; }
        [Display(Name = "Trạng thái")]
        public bool? IsActive { get; set; }
    }
}

发布课程:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace xxx.Areas.admin.Models
{
    public abstract class Post : Generic
    {
        public string Title { get; set; }
        public string Slug { get; set; }
        public string Content { get; set; }
        public string Image { get; set; }
        public int Views { get; set; }
        public bool? AllowComment { get; set; }

        public User ModifiedBy { get; set; }
        public virtual ICollection<Media> Medias { get; set; }
    }
}

AlbumBase类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using xxx.Areas.admin.Models.SongBase;

namespace xxx.Areas.admin.Models.AlbumBase
{
    public abstract class AlbumBase : Post
    {
        public bool IsPublic { get; set; }
        public bool IsFeatured { get; set; }

        public int OldID { get; set; }
        public string OldSlug { get; set; }

        public virtual ICollection<Comment> Comments { get; set; }
    }
}

播放列表类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using xxx.Areas.admin.Models.SongBase;

namespace xxx.Areas.admin.Models.AlbumBase
{
    public class Playlist : AlbumBase
    {
        [Key]
        public int PlaylistID { get; set; }

        public virtual ICollection<Song> Songs { get; set; }
        public virtual ICollection<Folk> Folks { get; set; }
        public virtual ICollection<Instrumental> Instrumentals { get; set; }
        public virtual User User { get; set; }

        public Playlist()
        { }

        public Playlist(string name)
        {
            Title = name;
            Slug = Functions.URLFriendly(Title);
            Views = 0;
            OldID = 0;
            AllowComment = true;
            IsActive = true;
            IsPublic = false;
            IsFeatured = false;
            Created = DateTime.Now;
        }
    }
}

和用户类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using baicadicungnamthang.Areas.admin.Models.AlbumBase;
using baicadicungnamthang.Areas.admin.Models.Social;
using baicadicungnamthang.DAL;
using ICB;

namespace xxx.Areas.admin.Models
{
    public class User : Generic
    {
        [Key]
        public int UserID { get; set; }
        [Required(ErrorMessage = "Bạn phải nhập tên tài khoản"), StringLength(50)]
        public string UserName { get; set; }
        public string Password { get; set; }
        public string HashPassword { get; set; }
        [Required(ErrorMessage = "Bạn phải nhập địa chỉ email"), EmailAddress(ErrorMessage = "Địa chỉ email không hợp lệ")]
        public string Email { get; set; }
        [StringLength(50)]
        public string NickName { get; set; }
        public string FullName { get; set; }
        public string Slug { get; set; }
        public string Title { get; set; }
        public string Phone { get; set; }
        public string Avatar { get; set; }
        public DateTime? DOB { get; set; }
        [StringLength(1)]
        public string Gender { get; set; }
        public string Address { get; set; }
        public int TotalLikes { get; set; }
        public int TotalComments { get; set; }
        public int Views { get; set; }
        public string ActivationKey { get; set; }
        public string RecoverKey { get; set; }
        public DateTime? LastLogin { get; set; }
        public int OldID { get; set; }

        public virtual Role Role { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
        public virtual ICollection<Comment> RateComments { get; set; }
        public virtual ICollection<Playlist> Playlists { get; set; }
        public virtual ICollection<User> Friends { get; set; }
        public virtual ICollection<Message> MessagesSent { get; set; }
        public virtual ICollection<Message> MessagesReceived { get; set; }

        public User()
        {
            Created = DateTime.Now;
            IsActive = false;
            TotalLikes = 0;
            Views = 0;
            OldID = 0;
        }

        public string getAvatar(int w, int h)
        {
            return Functions.getAvatarThumb(UserName, w, h);
        }

        public int getAge()
        {
            if (DOB == null)
            {
                return 0;
            }
            else
            {
                DateTime now = DateTime.Now;
                int age = now.Year - DOB.Value.Year;
                return age;
            }
        }

        public string getGender()
        {
            if (Gender == "M")
            {
                return "Nam";
            }
            else if (Gender == "F")
            {
                return "Nữ";
            }
            else return "";
        }
    }
}

这是首先从代码生成的播放列表: Entity framework has generated two foreign key column of one key

如您所见,实体框架已生成两列:User_UserID和User_UserID1,来自User表的主键UserID。

我这样说是因为当我取消注释该行时     // public virtual User User {get;组; } 并重建项目,两列User_UserID和User_UserID1也已消失。

问题只发生在用户和播放列表关系中。使用其他一对多方案(用户注释),系统运行良好。

有人可以给我一个建议吗?

1 个答案:

答案 0 :(得分:1)

问题在于您在同一实体之间存在多种关系。

Playlist有2个User引用(一个在Playlist类中,一个在其基类Post中)。

这会混淆Entity Framework,因为它不知道如何映射关系,这就是它在数据库中创建太多外键的原因。

要解决此问题,您可以使用InverseProperty属性告诉它如何映射导航属性:

public class Playlist : AlbumBase
{
    [Key]
    public int PlaylistID { get; set; }

    [InverseProperty("Playlists")]
    public virtual User User { get; set; }
    ......