询问者
Entity Framework 问题 Type name xxxx is already defined.

问题
-
我用Entity Framework Power Tools 的 CodeFirst 生成的 Model层。
但我在使用的时候出现异常错误。错误如下:
One or more validation errors were detected during model generation: System.Data.Edm.EdmEntityType: Name: Each type name in a schema must be unique. Type name 'CUSTOMER_LOCATION' is already defined. System.Data.Edm.EdmEntitySet: Name: Each member name in an EntityContainer must be unique. A member with name 'CUSTOMER_LOCATION' is already defined.
在 System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer) 在 System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) 在 System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) 在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) 在 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) 在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) 在 System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() 在 System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() 在 System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() 在 System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate) 在 Repository.CrudRepository`1.GetByQuery(Expression`1 exp) 位置 D:\GavinHomeWork\PowerTMS\MobileTMS\WCFService\MobileService\Repository\CrudRepository.cs:行号 58 在 Service.CrudService`1.GetByQuery(Expression`1 exp) 位置 D:\GavinHomeWork\PowerTMS\MobileTMS\WCFService\MobileService\Service\CrudService.cs:行号 49 在 MobileService.SOrder.GetNotConfirmCount(ResponseUser user) 位置 D:\GavinHomeWork\PowerTMS\MobileTMS\WCFService\MobileService\MobileService\SOrder.cs:行号 38 在 SyncInvokeGetNotConfirmCount(Object , Object[] , Object[] ) 在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
我只是查询了一下Order的实体,并没有查询CONTACT_LOCATION. 但还是出现了这个错误。
我的CONTACT 和 LOCATION 是多对多关系,所以用了 CONTACT_LOCATION 表记录关系。
这是我实体的内容
public class CONTACT { public CONTACT() { this.CUSTOMERs = new List<CUSTOMER>(); } public string CONTACT_GID { get; set; } public string CONTACT_ID { get; set; } public string CORPORATION_GID { get; set; } public string LOCATION_GID { get; set; } public string FIRST_NAME { get; set; } public string LAST_NAME { get; set; } public string JOB_TITLE { get; set; } public string EMAIL { get; set; } public string IS_TEMP0RARY { get; set; } public string LANGUAGE { get; set; } public string PHONE1 { get; set; } public string FAX { get; set; } public string PHONE2 { get; set; } public string TELEPHONE { get; set; } public string REMARK { get; set; } public string CREATED_BY { get; set; } public System.DateTime CREATED_DATE { get; set; } public string UPDATED_BY { get; set; } public Nullable<System.DateTime> UPDATED_DATE { get; set; } public string DOMAIN_NAME { get; set; } public Nullable<bool> IS_LOCAL { get; set; } public Nullable<bool> IS_BUSINESS { get; set; } public virtual CORPORATION CORPORATION { get; set; } public virtual LOCATION LOCATION { get; set; } public virtual ICollection<CUSTOMER> CUSTOMERs { get; set; } }
public class CONTACT_LOCATION { public string CONTACT_GID { get; set; } public string LOCATION_GID { get; set; } public Nullable<bool> IS_DEFAULT_LOCATION { get; set; } }
public class CONTACTMap : EntityTypeConfiguration<CONTACT> { public CONTACTMap() { // Primary Key this.HasKey(t => t.CONTACT_GID); // Properties this.Property(t => t.CONTACT_GID) .IsRequired() .HasMaxLength(101); this.Property(t => t.CONTACT_ID) .IsRequired() .HasMaxLength(50); this.Property(t => t.CORPORATION_GID) .HasMaxLength(101); this.Property(t => t.LOCATION_GID) .HasMaxLength(101); this.Property(t => t.FIRST_NAME) .HasMaxLength(128); this.Property(t => t.LAST_NAME) .HasMaxLength(50); this.Property(t => t.JOB_TITLE) .HasMaxLength(50); this.Property(t => t.EMAIL) .HasMaxLength(50); this.Property(t => t.IS_TEMP0RARY) .HasMaxLength(1); this.Property(t => t.LANGUAGE) .HasMaxLength(50); this.Property(t => t.PHONE1) .HasMaxLength(50); this.Property(t => t.FAX) .HasMaxLength(50); this.Property(t => t.PHONE2) .HasMaxLength(50); this.Property(t => t.TELEPHONE) .HasMaxLength(50); this.Property(t => t.REMARK) .HasMaxLength(200); this.Property(t => t.CREATED_BY) .IsRequired() .HasMaxLength(128); this.Property(t => t.UPDATED_BY) .HasMaxLength(128); this.Property(t => t.DOMAIN_NAME) .IsRequired() .HasMaxLength(50); // Table & Column Mappings this.ToTable("CONTACT"); this.Property(t => t.CONTACT_GID).HasColumnName("CONTACT_GID"); this.Property(t => t.CONTACT_ID).HasColumnName("CONTACT_ID"); this.Property(t => t.CORPORATION_GID).HasColumnName("CORPORATION_GID"); this.Property(t => t.LOCATION_GID).HasColumnName("LOCATION_GID"); this.Property(t => t.FIRST_NAME).HasColumnName("FIRST_NAME"); this.Property(t => t.LAST_NAME).HasColumnName("LAST_NAME"); this.Property(t => t.JOB_TITLE).HasColumnName("JOB_TITLE"); this.Property(t => t.EMAIL).HasColumnName("EMAIL"); this.Property(t => t.IS_TEMP0RARY).HasColumnName("IS_TEMP0RARY"); this.Property(t => t.LANGUAGE).HasColumnName("LANGUAGE"); this.Property(t => t.PHONE1).HasColumnName("PHONE1"); this.Property(t => t.FAX).HasColumnName("FAX"); this.Property(t => t.PHONE2).HasColumnName("PHONE2"); this.Property(t => t.TELEPHONE).HasColumnName("TELEPHONE"); this.Property(t => t.REMARK).HasColumnName("REMARK"); this.Property(t => t.CREATED_BY).HasColumnName("CREATED_BY"); this.Property(t => t.CREATED_DATE).HasColumnName("CREATED_DATE"); this.Property(t => t.UPDATED_BY).HasColumnName("UPDATED_BY"); this.Property(t => t.UPDATED_DATE).HasColumnName("UPDATED_DATE"); this.Property(t => t.DOMAIN_NAME).HasColumnName("DOMAIN_NAME"); this.Property(t => t.IS_LOCAL).HasColumnName("IS_LOCAL"); this.Property(t => t.IS_BUSINESS).HasColumnName("IS_BUSINESS"); // Relationships this.HasOptional(t => t.CORPORATION) .WithMany(t => t.CONTACTs) .HasForeignKey(d => d.CORPORATION_GID); this.HasOptional(t => t.LOCATION) .WithMany(t => t.CONTACTs) .HasForeignKey(d => d.LOCATION_GID); } }
public class CONTACT_LOCATIONMap : EntityTypeConfiguration<CONTACT_LOCATION> { public CONTACT_LOCATIONMap() { // Primary Key this.HasKey(t => new { t.CONTACT_GID, t.LOCATION_GID }); // Properties this.Property(t => t.CONTACT_GID) .IsRequired() .HasMaxLength(101); this.Property(t => t.LOCATION_GID) .IsRequired() .HasMaxLength(101); // Table & Column Mappings this.ToTable("CONTACT_LOCATION"); this.Property(t => t.CONTACT_GID).HasColumnName("CONTACT_GID"); this.Property(t => t.LOCATION_GID).HasColumnName("LOCATION_GID"); this.Property(t => t.IS_DEFAULT_LOCATION).HasColumnName("IS_DEFAULT_LOCATION"); } }
请告诉我,应该怎么做才能解决这个问题呢?非常感谢补充一下:我实体好像贴错了。
public CUSTOMER_LOCATIONMap() { // Primary Key this.HasKey(t => new { t.CUSTOMER_GID, t.LOCATION_GID }); // Properties this.Property(t => t.CUSTOMER_GID) .IsRequired() .HasMaxLength(101); this.Property(t => t.LOCATION_GID) .IsRequired() .HasMaxLength(101); this.Property(t => t.CUSTOMER_BACK_TYPE) .HasMaxLength(200); this.Property(t => t.CORPORATION_BACK_TYPE) .HasMaxLength(200); // Table & Column Mappings this.ToTable("CUSTOMER_LOCATION"); this.Property(t => t.CUSTOMER_GID).HasColumnName("CUSTOMER_GID"); this.Property(t => t.LOCATION_GID).HasColumnName("LOCATION_GID"); this.Property(t => t.IS_DEFAULT_LOCATION).HasColumnName("IS_DEFAULT_LOCATION"); this.Property(t => t.IS_SOURCE).HasColumnName("IS_SOURCE"); this.Property(t => t.IS_DEST).HasColumnName("IS_DEST"); this.Property(t => t.IS_CUSTOMER_BACK).HasColumnName("IS_CUSTOMER_BACK"); this.Property(t => t.CUSTOMER_BACK_TYPE).HasColumnName("CUSTOMER_BACK_TYPE"); this.Property(t => t.IS_CORPORATION_BACK).HasColumnName("IS_CORPORATION_BACK"); this.Property(t => t.CORPORATION_BACK_TYPE).HasColumnName("CORPORATION_BACK_TYPE"); }
public class CUSTOMER_LOCATION { public string CUSTOMER_GID { get; set; } public string LOCATION_GID { get; set; } public Nullable<bool> IS_DEFAULT_LOCATION { get; set; } public Nullable<bool> IS_SOURCE { get; set; } public Nullable<bool> IS_DEST { get; set; } public Nullable<bool> IS_CUSTOMER_BACK { get; set; } public string CUSTOMER_BACK_TYPE { get; set; } public Nullable<bool> IS_CORPORATION_BACK { get; set; } public string CORPORATION_BACK_TYPE { get; set; } }
public class CUSTOMER { public CUSTOMER() { this.BACK_TO_COMPANY = new List<BACK_TO_COMPANY>(); } public string CUSTOMER_GID { get; set; } public string ANTICIPATE_UOM { get; set; } public virtual ICollection<BACK_TO_COMPANY> BACK_TO_COMPANY { get; set; } public virtual CONTACT CONTACT { get; set; } public virtual CORPORATION CORPORATION { get; set; } public virtual LOCATION LOCATION { get; set; } }
public class CUSTOMERMap : EntityTypeConfiguration<CUSTOMER> { public CUSTOMERMap() { // Primary Key this.HasKey(t => t.CUSTOMER_GID); // Properties this.Property(t => t.CUSTOMER_GID) .IsRequired() .HasMaxLength(101); this.Property(t => t.ANTICIPATE_UOM).HasColumnName("ANTICIPATE_UOM"); // Relationships this.HasOptional(t => t.CONTACT) .WithMany(t => t.CUSTOMERs) .HasForeignKey(d => d.CONTACT_GID); this.HasOptional(t => t.CORPORATION) .WithMany(t => t.CUSTOMERs) .HasForeignKey(d => d.CORPORATION_GID); this.HasOptional(t => t.LOCATION) .WithMany(t => t.CUSTOMERs) .HasForeignKey(d => d.MASTER_LOCATION_GID); }
我检查了所有Model,CUSTOMER_LOCATION 只定义过一次,为什么提示我已经定义了呢?
Welcome to Gavin Luo `s blog
- 已编辑 Gavin Luo 2011年8月10日 10:08 补充
全部回复
-
请告诉我一下
System.Data.Edm.EdmEntityType: Name: Each type name in a schema must be unique. Type name 'CUSTOMER_LOCATION' is already defined. System.Data.Edm.EdmEntitySet: Name: Each member name in an EntityContainer must be unique. A member with name 'CUSTOMER_LOCATION' is already defined.
这两句错误他到底能给我什么提示?
Welcome to Gavin Luo `s blog -
你的DBContext里面不会已经有CUSTOMER_LOCATION了吧?
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 冯瑞涛Moderator 2012年4月30日 7:06
- 取消答案标记 冯瑞涛Moderator 2012年4月30日 7:06