0

我在 ASP.Net MVC 5 中使用 Entity Framework 6 Model First 时遇到了一个奇怪的问题。

我有一个学生班和一个家庭班。一个家庭可以有很多学生。

我可以毫无问题地添加一个家庭,但是当我为该家庭添加一个学生时,它会使用不同的键创建两条记录。

这是我的模型:

     public partial class Family
{
    public Family()
    {
        this.Students = new HashSet<Student>();
        this.Contacts = new HashSet<Contact>();
    }

    public System.Guid FamilyId { get; set; }
    public string PayerType { get; set; }
    public bool ProblemAccount { get; set; }
    public bool Active { get; set; }
    public string CustomId { get; set; }
    public string FamilyName { get; set; }
    public string Note { get; set; }

    public virtual ICollection<Student> Students { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
}

   public partial class Student
{
    public Student()
    {
        this.Phones = new HashSet<Phone>();
        this.Addresses = new HashSet<Address>();
        this.Classes = new HashSet<Class>();
    }

    public System.Guid StudentId { get; set; }
    public string Name { get; set; }
    public string Last { get; set; }
    public System.Guid FamilyFamilyId { get; set; }
    public string CustomId { get; set; }
    public string Email { get; set; }
    public bool MassEmail { get; set; }
    public string Gender { get; set; }
    public string Transportation { get; set; }
    public string School { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> EndDate { get; set; }
    public Nullable<System.DateTime> BirthDate { get; set; }
    public bool Active { get; set; }
    public string Note { get; set; }
    public bool Citizenship { get; set; }

    public virtual Family Family { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Phone> Phones { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Class> Classes { get; set; }
}

这是我添加学生的控制器:

   public async Task<ActionResult> AddNewStudent(Guid FamilyId, NewRegistrationViewModel model)
    {

        model.Student.StudentId = Guid.NewGuid();
        model.Student.FamilyFamilyId = FamilyId;
        db.Students.Add(model.Student);

        await db.SaveChangesAsync();

        return View();
    }

我使它不重复的唯一方法是不为 StudentId 生成新的 guid。如果我评论我的 AddNewStudent 控制器的第一行,它会保存好,但没有 guid,当然我们不希望这样。

像这样:

  public async Task<ActionResult> AddNewStudent(Guid FamilyId, NewRegistrationViewModel model)
    {

        //model.Student.StudentId = Guid.NewGuid();
        model.Student.FamilyFamilyId = FamilyId;
        db.Students.Add(model.Student);

        await db.SaveChangesAsync();

        return View();
    }
4

1 回答 1

0

您需要通过 Fluent API或Data Annotations使用配置/映射属性和类型来使用某种类型的数据库映射。

数据注释看起来像这样。

[Table("Family")
public partial class Family
{
[Key]
public System.Guid FamilyId { get; set; }
---

Fluent Mapping看起来像这样并进入OnModelCreating(可能在 YourDbContext.cs 中)

modelBuilder.Entity<Family>().HasKey(t => t.FamilyId);
于 2013-11-14T21:39:29.587 回答