2013-01-23 29 views
0

在我的合同表中创建了一些外键。 (文章和客户)。公司没关系!数据库中的外键复制

我的模型:

public class Contract { 
    [Key] 
    public int ContractID { get; set; } 
    public double PricePerUnit { get; set; } 
    public int Unit { get; set; } 
    public int Currency { get; set; } 

    [Required] 
    public int ClientID { get; set; } 
    public virtual Client Client { get; set; } 

    [Required] 
    public int CompanyID { get; set; } 
    public virtual Company Company { get; set; } 

    [Required] 
    public int ArticleID { get; set; } 
    public virtual Article Article { get; set; } 

} 

public class Client { 
    [Key] 
    public int ClientID { get; set; } 
    public string Number { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string ZipCode { get; set; } 
    public string City { get; set; } 
    public string AddressLine1 { get; set; } 
    public string AddressLine2 { get; set; } 
    public string Memo { get; set; } 
    public bool isMerchant { get; set; } 

    public string Name 
    { 
     get 
     { 
      return string.Format("{0} {1}", FirstName, LastName); 
     } 
    } 

    //[Required] 
    public int? MerchantReferenceID { get; set; } 
    public virtual Client MerchantReference { get; set; } 
    [Required] 
    public int CompanyID { get; set; } 
    public virtual Company Company { get; set; } 
    public virtual ICollection<Contract> Contracts { get; set; } 
    public virtual ICollection<Order> Orders { get; set; } 
} 

public class Company 
{ 
    [Key] 
    public int CompanyID { get; set; } 
    public string Name { get; set; } 
    public int DeviceIncomingWeight { get; set; } 
    public string ZipCode { get; set; } 
    public string AddressLine1 { get; set; } 
    public string AddressLine2 { get; set; } 
    public string City { get; set; } 
    public bool Admin { get; set; } 
    public int UnitForMeasurements { get; set; } 
    public int UnitForDisplayOnDocuments { get; set; } 

    public virtual ICollection<User> Users { get; set; } 
    public virtual ICollection<Category> Categories { get; set; } 
    public virtual ICollection<Article> Articles { get; set; } 
    public virtual ICollection<Client> Clients { get; set; } 
    public virtual ICollection<Location> Locations { get; set; } 
    public virtual ICollection<Contract> Contracts { get; set; } 
    public virtual ICollection<IncomingMeasurement> IncomingMeasurements { get; set; } 
    public virtual ICollection<Measurement> Measurements { get; set; } 
    public virtual ICollection<Order> Orders { get; set; } 
} 

public class Article { 
    [Key] 
    public int ArticleID { get; set; } 
    [Required] 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public bool TrackStock { get; set; } 
    public int CurrentStock { get; set; } 
    public double? Price { get; set; } 

    [Required] 
    public int CompanyID { get; set; } 
    public virtual Company Company { get; set; } 
    [Required] 
    public int CategoryID { get; set; } 
    public virtual Category Category { get; set; } 

    public virtual ICollection<Contract> Contracts { get; set; } 
    public virtual ICollection<Order> Orders { get; set; } 
} 

这是我OnModelCreating,其中可能故障的所在:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     // modelBuilder.Entity<Contract>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<Contract>().HasRequired(bm => bm.Article).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<Contract>().HasRequired(bm => bm.Client).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<Article>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<Measurement>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<Order>().HasRequired(bm => bm.Client).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<Order>().HasRequired(bm => bm.Article).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<IncomingMeasurement>().HasRequired(bm => bm.client).WithMany().WillCascadeOnDelete(false); 
     modelBuilder.Entity<Client>().HasOptional(c => c.MerchantReference).WithMany().HasForeignKey(c => c.MerchantReferenceID); 

     //Required fields 


     base.OnModelCreating(modelBuilder); 
    } 

而且还有一些奇怪的事情发生在我的数据库(我的SQL Server),即这是我创建表架构。

这些都是我的领域:

CREATE TABLE [dbo].[Contracts](
[ContractID] [int] IDENTITY(1,1) NOT NULL, 
[PricePerUnit] [float] NOT NULL, 
[Unit] [int] NOT NULL, 
[Currency] [int] NOT NULL, 
[ClientID] [int] NOT NULL, 
[CompanyID] [int] NOT NULL, 
[ArticleID] [int] NOT NULL, 
[Client_ClientID] [int] NOT NULL, 
[Article_ArticleID] [int] NOT NULL, 
[Client_ClientID1] [int] NULL, 
[Article_ArticleID1] [int] NULL, 

如果你注意到它,[Client_ClientID]有重复:[Client_ClientID1]也[Article_ArticleID]在[Article_ArticleID1。 但公司没有。

有关如何解决这个问题的任何想法?

回答

1

发生这种情况是因为您在实体类中包含冗余(外键)列。例如,查看Contract课程中的类别。

public class Contract 
{ 
    public Int32 CategoryID { get; set; } 

    public virtual Category Category { get; set; } 
} 

您手动指定的财产,因此列CategoryID然后实体框架生成另一列持有因财产Category引用的Category外键。

所以只要删除属性CategoryID并使用contract.Category.CategoryID而不是如果您需要引用类别的ID。

UPDATE

我是不知道的建议,包括外键属性,但看着杰夫西瑞尔的答案的评论链接的文章中,我可能发现在部分答案配置非常规外键名字

实体框架使用惯例相匹配的导航属性的名称和外键属性和默认的约定是要么NavigationPropertyNameIdNavigationPropertyName_Id当你使用NavigationPropertyNameID用大写D

因此,您有几个选择 - 将您的命名更改为使用Id,替换约定或重写约定。

+0

请参阅我对其他答案的评论:) – NicoJuicy

+0

我阅读它和我希望它有助于发现问题 - 请参阅我的更新。 –

+0

工作以更改视图,模型,代码,... ;-)将尽快更新! – NicoJuicy

0

消除模型中的重复信息。引用对象的id不是必需的,是什么导致你的问题。

public class Contract { 
    [Key] 
    public int ContractID { get; set; } 
    public double PricePerUnit { get; set; } 
    public int Unit { get; set; } 
    public int Currency { get; set; } 

    public virtual Client Client { get; set; } 

    public virtual Company Company { get; set; } 

    public virtual Article Article { get; set; } 
} 

而不是id上的Required属性,您需要设置您的实体,以便需要子项。

+0

我曾经这样做过,但所有官方消息都表示最好还是将int作为外键添加。例如。 http://msdn.microsoft.com/en-us/data/hh134698.aspx。我也看到了脚手架上的一些改进,这可能是一个迹象,为什么它更好:) 我在文章和客户端脚手架问题,这正是我现在有这个问题。 我不认为这是真正的解决方案,在过去做过,它不会在控制器+视图中产生脚手架,所以直到证明错误,我正在寻找更好的答案。 – NicoJuicy

+0

PS。即使斯科特Hanselmans示例项目这样做,我这样做(虽然我有一个错误的地方)。 http://nerddinner.codeplex.com/SourceControl/changeset/view/b1a032d1532b#mvc4/NerdDinner/Models/RSVP.cs – NicoJuicy

+0

刚刚看到您的OnModelCreating。你为什么指定这些属性需要两次?您已获得相应属性的必需属性,然后在模型创建中指定HasRequired。我相信这是造成这个问题的原因。特别是因为公司没有问题,并且合同公司的HasRequired生产线被注释掉了。 –