问题
ANSWERED! (will be posting it shortly. I seriously think it's a bug in EF.)
I have the following set of (code-first) classes which will be the future of the application as we are replacing the old (gen'd from db). The dbcontext, two mappings/configurations, two POCOs, and a base class.
In addition to this in another project entirely is an edmx generated from the database. Up until now, there have not been any issues with the two conflicting. There are several other unrelated sets in the context (not shown,) but it wasn't until I started trying to get the relationship between SalesReps
and SalesGroups
that I started having issues. Commenting out adding the SalesGroupMapping
in OnModelCreating
will allow it to run.
The Error:
Schema specified is not valid. Errors:
(19,6) : error 0019: Each property name in a type must be unique. Property name 'PKID_MasterRepGroup' was already defined.
(27,6) : error 0019: Each property name in a type must be unique. Property name 'PKID_MasterRepGroup' was already defined.
The Code:
public class MyDbContext{
public IDbSet<SalesGroup> SalesGroups { get; set; }
public IDbSet<SalesRep> SalesReps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new InsuranceProviderMapping());
modelBuilder.Configurations.Add(new SalesGroupMapping());
modelBuilder.Configurations.Add(new SalesRepMapping());
base.OnModelCreating(modelBuilder);
}
}
class SalesRepMapping : EntityTypeConfiguration<SalesRep>
{
public SalesRepMapping()
{
ToTable("SalesRep");
Property(p => p.Id).HasColumnName("PKID_SalesRep");
Property(p => p.Email).HasColumnName("EMailAddress1");
Property(p => p.Address.Address1).HasColumnName("Address");
Property(p => p.Address.Address2).HasColumnName("Address2");
Property(p => p.Address.City).HasColumnName("City");
Property(p => p.Address.State).HasColumnName("State");
Property(p => p.Address.Zipcode).HasColumnName("Zipcode");
//Property(p => p.SalesGroupId).HasColumnName("FK_MasterRepGroup");
//HasRequired(p => p.SalesGroup).WithMany(c => c.SalesReps).HasForeignKey(b => b.SalesGroupId);
//Note: dropping emailaddress2
}
}
class SalesGroupMapping : EntityTypeConfiguration<SalesGroup>
{
public SalesGroupMapping()
{
ToTable("MasterRepGroup");
Property(p => p.Id).HasColumnName("PKID_MasterRepGroup");
Property(p => p.Name).HasColumnName("CompanyName");
Property(p => p.PrimaryContact.FirstName).HasColumnName("Contact1FirstName");
Property(p => p.PrimaryContact.LastName).HasColumnName("Contact1LastName");
Property(p => p.PrimaryContact.Phone).HasColumnName("Contact1Phone");
Property(p => p.PrimaryContact.Fax).HasColumnName("Contact1Fax");
Property(p => p.PrimaryContact.Email).HasColumnName("Contact1EMail");
Property(p => p.SecondaryContact.FirstName).HasColumnName("Contact2FirstName");
Property(p => p.SecondaryContact.LastName).HasColumnName("Contact2LastName");
Property(p => p.SecondaryContact.Phone).HasColumnName("Contact2Phone");
Property(p => p.SecondaryContact.Fax).HasColumnName("Contact2Fax");
Property(p => p.SecondaryContact.Email).HasColumnName("Contact2EMail");
Property(p => p.Address.Address1).HasColumnName("Address");
Property(p => p.Address.Address2).HasColumnName("Address2");
Property(p => p.Address.City).HasColumnName("City");
Property(p => p.Address.State).HasColumnName("State");
Property(p => p.Address.Zipcode).HasColumnName("Zipcode");
}
}
public class SalesRep: Entity
{
virtual public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Phone]
virtual public string Phone { get; set; }
[Phone]
virtual public string Fax { get; set; }
[Email]
virtual public string Email { get; set; }
virtual public string Notes { get; set; }
virtual public Address Address { get; set; }
//public long SalesGroupId { get; set; }
//[Required]
////[ForeignKey("SalesGroupId")]
//virtual public SalesGroup SalesGroup { get; set; }
}
public class SalesGroup : Entity
{
public SalesGroup()
{
//SalesReps = new List<SalesRep>();
}
[Required]
public string Name { get; set; }
virtual public Address Address { get; set; }
virtual public Contact PrimaryContact { get; set; }
virtual public Contact SecondaryContact { get; set; }
//virtual public ICollection<SalesRep> SalesReps {get; set;}
}
public abstract class Entity : NMTC.Core.DataContracts.IEntity
{
// TODO database columns will eventually be int instead of bigint
public long Id { get; set; }
}
}
Any ideas? I've been banging my head on this one since Friday. If you know of a way I can track down what it's thinking, that would help, too.
Much appreciated.
More Info:
Commenting out the mapping for the Id property alone will allow the model to be created. That won't actually help long-term, though.
回答1:
The error is a problem with EF.
In the mapping, I map Property(p=>p.Id).HasColumnName("MyId");
In my Contact class, which inherits from my Entity
class, there are also IDs.
Because I did not explicitly map these columns, the mapping "trickled down" and was trying to map my Contact.Id
properties in the same fashion.
Solutions:
a) Map them explicitly
b) Remove the inheritance so that Contact
isn't of type Entity
and is instead a ComplexType
(doesn't have an ID.)
来源:https://stackoverflow.com/questions/7641027/error-0019-each-property-name-in-a-type-must-be-unique-on-id-field