Asp.net mvc 4 实体框架代码首先生成数据库不正确
我认为实体框架在我的项目中生成数据库时有问题。奇怪的是,它只发生在一种情况下。这是“用户”和“播放列表”之间的一对多关系。一个用户有多个播放列表 这是我的代码,我在项目中使用了一些抽象类。 核心代码 播放列表类Asp.net mvc 4 实体框架代码首先生成数据库不正确,asp.net-mvc-4,ef-code-first,one-to-many,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.C
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 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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
命名空间xxx.Areas.admin.Models
{
公共抽象类Post:Generic
{
公共字符串标题{get;set;}
公共字符串Slug{get;set;}
公共字符串内容{get;set;}
公共字符串图像{get;set;}
公共int视图{get;set;}
公共bool?AllowComment{get;set;}
公共用户通过{get;set;}修改
公共虚拟ICollection媒体{get;set;}
}
}
基本类:
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用xxx.Areas.admin.Models.SongBase;
命名空间xxx.Areas.admin.Models.AlbumBase
{
公共抽象类库:Post
{
公共bool IsPublic{get;set;}
公共布尔值为{get;set;}
public int OldID{get;set;}
公共字符串oldslag{get;set;}
公共虚拟ICollection注释{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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
使用xxx.Areas.admin.Models.SongBase;
命名空间xxx.Areas.admin.Models.AlbumBase
{
公共类播放列表:AlbumBase
{
[关键]
public int playlyid{get;set;}
公共虚拟ICollection歌曲{get;set;}
公共虚拟ICollection{get;set;}
公共虚拟ICollection工具{get;set;}
公共虚拟用户用户{get;set;}
公共播放列表()
{ }
公共播放列表(字符串名称)
{
Title=名称;
Slug=Functions.URLFriendly(标题);
视图=0;
OldID=0;
AllowComment=true;
IsActive=true;
IsPublic=假;
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 "";
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
使用baicadicunnamtang.Areas.admin.Models.AlbumBase;
使用baicadicunnamtang.Areas.admin.Models.Social;
使用黄芩提取物;
使用ICB;
命名空间xxx.Areas.admin.Models
{
公共类用户:泛型
{
[关键]
public int UserID{get;set;}
[必需(ErrorMessage=“Bạn phải nhậpên tái khoả长度(50)]
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
公共字符串HashPassword{get;set;}
[必需(ErrorMessage=“Bạn phải nhậpđị阿奇ỉ 电子邮件),电子邮件地址(ErrorMessage=“Đ”ị阿奇ỉ 电子邮件khonghợPLệ")]
公共字符串电子邮件{get;set;}
[长度(50)]
公共字符串昵称{get;set;}
公共字符串全名{get;set;}
公共字符串Slug{get;set;}
公共字符串标题{get;set;}
公用字符串电话{get;set;}
公共字符串Avatar{get;set;}
公共日期时间?DOB{get;set;}
[第(1)款]
公共字符串{get;set;}
公共字符串地址{get;set;}
公共整数totalikes{get;set;}
公共int TotalComments{get;set;}
公共int视图{get;set;}
公共字符串激活密钥{get;set;}
公共字符串恢复密钥{get;set;}
公共日期时间?LastLogin{get;set;}
public int OldID{get;set;}
公共虚拟角色{get;set;}
公共虚拟ICollection注释{get;set;}
公共虚拟ICollection RateComments{get;set;}
公共虚拟ICollection播放列表{get;set;}
公共虚拟ICollection好友{get;set;}
公共虚拟ICollection消息sent{get;set;}
收到的公共虚拟ICollection消息{get;set;}
公共用户()
{
Created=DateTime.Now;
IsActive=假;
Totalikes=0;
视图=0;
OldID=0;
}
公共字符串getAvatar(int w,int h)
{
返回函数.getAvatarThumb(用户名,w,h);
}
公共整数getAge()
{
如果(DOB==null)
{
返回0;
}
其他的
{
DateTime now=DateTime.now;
int age=now.Year-DOB.Value.Year;
回归年龄;
}
}
公共字符串getGender()
{
如果(性别=“M”)
{
返回“不结盟运动”;
}
否则如果(性别=“F”)
{
返回“N”ữ";
}
否则返回“”;
}
}
}
这是首先从代码生成的播放列表表:
正如您所看到的,实体框架从用户表的主键UserID生成了两列:User_UserID和User_UserID1
我这么说是因为当我取消注释这行时
//公共虚拟用户用户{get;set;}
重建项目时,两列User_UserID和User_UserID1也消失了
问题只发生在用户和播放列表的关系上。对于其他一对多场景(用户评论),系统运行良好
有谁能给我一个建议吗?问题是,你的团队之间有多重关系
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 "";
}
}
}
public class Playlist : AlbumBase
{
[Key]
public int PlaylistID { get; set; }
[InverseProperty("Playlists")]
public virtual User User { get; set; }
......