2013-05-01 205 views
1

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

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

Playlist类

public virtual User User { get; set; } 

User类

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; } 
    } 
} 

Post类:

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; } 
    } 
} 

Playlist类:

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从用户表的主键的用户ID。

我这样说是因为当我取消对该行 //公共虚拟用户用户{获得;组; } 并重建项目,这两列User_UserID和User_UserID1也消失了。

该问题只与用户和播放列表的关系发生了。与其他一对多方案(用户评论)相比,系统运作良好。

谁能给我一个建议?

回答

1

问题是你有相同的实体之间的多个关系。

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

这混淆了实体框架,因为它不知道如何将关系映射,这就是为什么它在数据库中创建了太多的外键。

为了解决这个问题,你可以使用InverseProperty属性来告诉它如何映射导航属性:

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

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

非常感谢您!我的问题解决了。 – 2013-05-01 16:33:21

相关问题