简体   繁体   English

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

[英]Entity Framework model error for generlize entity

i am using asp.net mvc5 app. 我正在使用asp.net mvc5应用程序。 I got generalize entities for following database structure that is diploma_markscheme and non_diploma_MarkScheme... 我得到了用于以下数据库结构的通用实体,即ployage_markscheme和non_diploma_MarkScheme ... 在此处输入图片说明

now i have model class as these table as 现在我有模型类作为这些表

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

at run time I get following Error I believe I am missing something or doing wrong in model class.... 在运行时,我遵循错误,我认为我在模型类中缺少某些东西或做错了...。

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.

Honestly, your database is kind of odd to me. 老实说,您的数据库对我来说有点奇怪。 Why is your primary key for DiplomaMarkScheme a foreign key to ElementMarkSchemeID? 为什么DiplomaMarkScheme的主键是ElementMarkSchemeID的外键? Anyway, it appears as though you are trying to achieve a one-to-one relationship, so please consult this document 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 wherein the proper models are configured like so: 无论如何,似乎您正在尝试建立一对一的关系,因此请查阅此文档http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef- 4-1-code-part-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; }
}

And add this override to your DbContext class: 并将此替代添加到您的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);
}

If you are trying, instead, to implement a different type of relationship, let me know and I'll try to help. 相反,如果您尝试实现其他类型的关系,请告诉我,我将尽力提供帮助。 You may want to consider restructuring your database in a more logical way, as I can't seem to understand the way you have it setup currently. 您可能需要考虑以更合乎逻辑的方式重组数据库,因为我似乎无法理解您当前设置数据库的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM