generlize实体的实体框架模型错误

时间:2014-04-02 14:34:25

标签: c# entity-framework

我正在使用asp.net mvc5应用程序。我得到了以下数据库结构的通用实体 那是diploma_markscheme和non_diploma_MarkScheme ...... enter image description here

现在我将模型类作为这些表作为

ElementMarkScheme

[Table("ElementMarkScheme")]
public class ElementMarkScheme : IElementMarkScheme
{
    public ElementMarkScheme()
    {

    }

    [Key]
    public int ElementMarkSchemeID { get; set; }

    public int QualificationElementID { get; set; }

    public int MarkSchemeID { get; set; }

    public DiplomaMarkScheme DiplomaMarkScheme { get; set; }
    public MarkScheme MarkScheme { get; set; }
    public Non_DiplomaMarkScheme Non_DiplomaMarkScheme { get; set; }
    public QualificationElement QualificationElement { get; set; }

}

Diploma_markscheme

[Table("DiplomaMarkScheme")]
public class DiplomaMarkScheme : IDiplomaMarkScheme 
{
    public DiplomaMarkScheme()
    {

    }

    [Key]
    public int ElementMarkSchemeID { get; set; }

    [Required(ErrorMessage = "Required Pass Mark")]
    [Display(Name = "Pass Mark")]
    public int PassMark { get; set; }


    [Display(Name = "Available Mark")]
    public int AvailableMark { get; set; }

    public ElementMarkScheme ElementMarkScheme { get; set; }

}

Non_Diploma_MarkScheme

 [Table("Non_DiplomaMarkScheme")]
public class Non_DiplomaMarkScheme : INon_DiplomaMarkScheme
{
    public Non_DiplomaMarkScheme()
    {

    }

    [Key]
    public int ElementMarkSchemeID { get; set; }

    [Required(ErrorMessage = "Required Pass Mark")]
    [Display(Name = "Pass Mark")]
    public int PassMark { get; set; }

    [Required(ErrorMessage = "Required Merit Mark")]
    [Display(Name = "Merit Mark")]
    public int MeritMark { get; set; }

    [Required(ErrorMessage = "Required Distinction Mark")]
    [Display(Name = "Distinction Mark")]
    public int DistinctionMark { get; set; }

    [Display(Name = "Available Mark")]
    public int AvailableMark { get; set; }

    public ElementMarkScheme ElementMarkScheme { get; set; }
}

在运行时我得到以下错误我相信我在模型类中缺少某些东西或做错了....

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Unable to determine the principal end of an association between the types 'LEO.DAL.Model.ElementMarkScheme' and 'LEO.DAL.Model.DiplomaMarkScheme'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

1 个答案:

答案 0 :(得分:1)

老实说,你的数据库对我来说有点奇怪。为什么DiplomaMarkScheme的主键是ElementMarkSchemeID的外键?无论如何,看起来好像你想要实现一对一的关系,所以请参考这个文档http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx,其中正确的模型配置如下:

public class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public int BillingAddressId { get; set; }
    public int DeliveryAddressId { get; set; }

    public Address BillingAddress { get; set; }
    public Address DeliveryAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
}

public class Context : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Address> Addresses { get; set; }
}

并将此覆盖添加到DbContext类:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasRequired(a => a.BillingAddress)
                .WithMany()
                .HasForeignKey(u => u.BillingAddressId);

    modelBuilder.Entity<User>()
                .HasRequired(a => a.DeliveryAddress)
                .WithMany()
                .HasForeignKey(u => u.DeliveryAddressId);
}

如果您正尝试实施不同类型的关系,请告诉我,我会尽力提供帮助。您可能需要考虑以更合理的方式重构数据库,因为我似乎无法理解您当前设置它的方式。