问题是我是实体框架的新手,我必须遗漏一些小事。
无论如何,我有三个表,我收到添加用户的错误:
关系:
1到1:用户 - 登录
1对多:用户 - ShippingProfiles
我的代码背后:
User user = new User();
user.AddUser(user, firstNameInput.Text, lastNameInput.Text, emailList, bdayInput.Text, emailInput.Text);
我的背景:
public class DBCS : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<ShippingProfile> ShippingProfiles { get; set; }
public DbSet<Login> Logins { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasRequired(user => user.ShippingProfiles)
.WithRequiredPrincipal();
}
}
所以我试图添加用户,我收到错误
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public Nullable<int> EmailList { get; set; }
public System.DateTime Birthday { get; set; }
public virtual Login Login { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ShippingProfile> ShippingProfiles { get; set; }
public void AddUser(User user,string firstNameInput, string lastNameInput, int emailListInput, string bdayInput, string emailInput)
{
using (var dbContext = new DBCS())
{
user.Birthday = Convert.ToDateTime(bdayInput);
user.Email = emailInput;
user.EmailList = Convert.ToInt32(emailListInput);
user.FirstName = firstNameInput;
user.LastName = lastNameInput;
dbContext.Users.Add(user); // Error Here
dbContext.SaveChanges();
}
}
这可能是一个愚蠢的问题,但我会感谢一些帮助。
提前致谢!
答案 0 :(得分:0)