일부 외래 키가 내 계약 테이블에 이중으로 생성됩니다. (기사 및 고객). 그리고 회사는 괜찮습니다!외래 키 데이터베이스에 중복 됨
내 모델 :이 오류가있다 아마 내 OnModelCreating입니다
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; }
}
:
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 서버)에있다, 즉이다 내 테이블 스키마를 만듭니다.
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]입니다. 하지만 회사는 그렇지 않습니다.이 문제를 해결하는 방법에 대한 의견이 있으십니까?
다른 대답에 대한 내 의견보기 :) – NicoJuicy
읽었습니다. 그리고이 문제를 발견하는 데 도움이되기를 바랍니다. 내 업데이트를 참조하십시오. –
보기, 모델, 코드, ... ;-)를 변경하면 곧 업데이트됩니다. – NicoJuicy