I have an abstract base class with a key property and two classes that inherit from this parent class as follows. When trying to persist objects to DB I get the following error messages. Can anyone guess what's wrong?
public abstract partial class Ad
{
public Ad()
{
this.Photos = new HashSet<Photo>();
this.Transactions = new HashSet<Transaction>();
this.Stats = new HashSet<Stat>();
}
public System.Guid AdID { get; set; }
// Some other properties here
}
public partial class SellingAd : Ad
{
}
public partial class BuyingAd : Ad
{
}
Inside my context class which inherits from DbContext I have
public partial class MyContainer : DbContext
{
private class SellingAdConfiguration : EntityTypeConfiguration<SellingAd>
{
public SellingAdConfiguration()
{
this.Map(m => m.MapInheritedProperties()).ToTable("SellingAd");
}
}
private class BuyingAdConfiguration : EntityTypeConfiguration<BuyingAd>
{
public BuyingAdConfiguration()
{
this.Map(m => m.MapInheritedProperties()).ToTable("BuyingAd");
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<MyContainer>(new MyDatabaseInitializationStrategy());
modelBuilder.Configurations.Add(new SellingAdConfiguration());
modelBuilder.Configurations.Add(new BuyingAdConfiguration());
modelBuilder.Entity<Ad>()
.Property(p => p.AdID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
While running this code and inserting some records into DB I get this error message
Violation of PRIMARY KEY constraint 'PK__SellingA__7130D58E1A14E395'. Cannot insert duplicate key in object 'dbo.SellingAd
I know I get this error because inherited classes have to have some identity column. Then I rewrite my private EntityTypeConfiguration classes like below:
this time I get the following error (although AdID is displayed by Intellisense while typing the code):
The property 'AdID' is not a declared property on type 'SellingAd'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive
property.
Also "AdID" column is created in both "SellingAd" & "BuyingAd" Tables.
The property 'AdID' is not a declared property on type 'SellingAd'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.
Please make sure that your AdID can be fetched by auto-intellisense context menu when you press a dot"."……。If not,I suggest you move the codes from the constructor one into OnModelCreating method。
kamran_saeed...
Member
74 Points
34 Posts
Inherited Property Not Recognized by EF
Feb 14, 2012 12:50 PM|LINK
Hi guys
I am using Code First approach (POCO).
I have an abstract base class with a key property and two classes that inherit from this parent class as follows. When trying to persist objects to DB I get the following error messages. Can anyone guess what's wrong?
public abstract partial class Ad { public Ad() { this.Photos = new HashSet<Photo>(); this.Transactions = new HashSet<Transaction>(); this.Stats = new HashSet<Stat>(); } public System.Guid AdID { get; set; } // Some other properties here } public partial class SellingAd : Ad { } public partial class BuyingAd : Ad { }Inside my context class which inherits from DbContext I have
public partial class MyContainer : DbContext { private class SellingAdConfiguration : EntityTypeConfiguration<SellingAd> { public SellingAdConfiguration() { this.Map(m => m.MapInheritedProperties()).ToTable("SellingAd"); } } private class BuyingAdConfiguration : EntityTypeConfiguration<BuyingAd> { public BuyingAdConfiguration() { this.Map(m => m.MapInheritedProperties()).ToTable("BuyingAd"); } } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer<MyContainer>(new MyDatabaseInitializationStrategy()); modelBuilder.Configurations.Add(new SellingAdConfiguration()); modelBuilder.Configurations.Add(new BuyingAdConfiguration()); modelBuilder.Entity<Ad>() .Property(p => p.AdID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); } }While running this code and inserting some records into DB I get this error message
Violation of PRIMARY KEY constraint 'PK__SellingA__7130D58E1A14E395'. Cannot insert duplicate key in object 'dbo.SellingAd
I know I get this error because inherited classes have to have some identity column. Then I rewrite my private EntityTypeConfiguration classes like below:
public SellingAdConfiguration() { this.Map(m => m.MapInheritedProperties()).ToTable("SellingAd"); this.HasKey(p => p.AdID); this.Property(p => p.AdID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); }this time I get the following error (although AdID is displayed by Intellisense while typing the code):
The property 'AdID' is not a declared property on type 'SellingAd'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.
Also "AdID" column is created in both "SellingAd" & "BuyingAd" Tables.
How can I get passed this?
Many thanks in advance.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Inherited Property Not Recognized by EF
Feb 16, 2012 12:36 AM|LINK
Please make sure that your AdID can be fetched by auto-intellisense context menu when you press a dot"."……。If not,I suggest you move the codes from the constructor one into OnModelCreating method。
Reguards!
kamran_saeed...
Member
74 Points
34 Posts
Re: Inherited Property Not Recognized by EF
Feb 16, 2012 08:50 AM|LINK
Decker
Intellisense shows it. Would you please take a look at this? http://stackoverflow.com/questions/9295901/inherited-primary-key-not-recognized-by-ef