Answered by:
EF 4.1 Code First - what is the equivalent of [NotMapped] in fluent API?

Question
-
I don't want to have depency on entity framework in my domain model and have mapping in a separate project:
this.Property(t => t.Id).HasColumnName("Id"); this.Property(t => t.CategoryId).HasColumnName("CategoryId");
and I would assume that all not explicitly mapped properties are not persisted. However, if I add a public property to my domain object, framework complains that corresponding column is missing in database. It appears that it is trying to do auto-mapping. This is very easy to fix with [NotMapped] data annotation attribute, but I don't want dependency on entity framework. I triedthis.Property(t => t.RetailPrice.Amount).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
but it didn't work. Can this be done without data annotation attributes?Thanks,-StanMonday, August 1, 2011 3:42 PM
Answers
-
Hi Stan,
Welcome!
Ignore a property: modelBuilder.Entity<Person>() .Ignore(p => p.Name);
the more information you can refer here: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspxHave a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Stan B Tuesday, August 2, 2011 9:46 PM
Tuesday, August 2, 2011 6:47 AM -
-
This works great. Thank you and "buaatt" .There is only one wrinkle, though.I have this code (generated by EF power tools, by the way):
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); modelBuilder.Configurations.Add(new StockItemMap()); }
Where map is like this:public class StockItemMap : EntityTypeConfiguration<StockItem> { public StockItemMap() { // Primary Key this.HasKey(t => t.Id); // Properties // Table & Column Mappings this.ToTable("StockItem"); this.Property(t => t.Id).HasColumnName("Id"); ////..
Ignore cannot be applied in the mapping class and I ended up puting in before:protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); modelBuilder.Configurations.Add(new CategoryMap()); modelBuilder.Configurations.Add(new StockItemMap()); modelBuilder.Entity<StockItem>().Ignore(s => s.RetailPrice); }
Which is I guess, something I am going to have to live with..
- Marked as answer by Alan_chen Wednesday, August 3, 2011 1:52 AM
Tuesday, August 2, 2011 10:02 PM
All replies
-
-
Hi Stan,
Welcome!
Ignore a property: modelBuilder.Entity<Person>() .Ignore(p => p.Name);
the more information you can refer here: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspxHave a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Stan B Tuesday, August 2, 2011 9:46 PM
Tuesday, August 2, 2011 6:47 AM -
This works great. Thank you and "buaatt" .There is only one wrinkle, though.I have this code (generated by EF power tools, by the way):
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); modelBuilder.Configurations.Add(new StockItemMap()); }
Where map is like this:public class StockItemMap : EntityTypeConfiguration<StockItem> { public StockItemMap() { // Primary Key this.HasKey(t => t.Id); // Properties // Table & Column Mappings this.ToTable("StockItem"); this.Property(t => t.Id).HasColumnName("Id"); ////..
Ignore cannot be applied in the mapping class and I ended up puting in before:protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); modelBuilder.Configurations.Add(new CategoryMap()); modelBuilder.Configurations.Add(new StockItemMap()); modelBuilder.Entity<StockItem>().Ignore(s => s.RetailPrice); }
Which is I guess, something I am going to have to live with..
- Marked as answer by Alan_chen Wednesday, August 3, 2011 1:52 AM
Tuesday, August 2, 2011 10:02 PM -
You can add it in the EntityTypeConfiguration<StockItem> Like this
public class StockItemMap : EntityTypeConfiguration<StockItem> { public StockItemMap() { // Primary Key this.HasKey(t => t.Id); // Properties // Table & Column Mappings this.ToTable("StockItem"); this.Property(t => t.Id).HasColumnName("Id"); Ignore(s => s.RetailPrice); ////..
- Edited by Joel Weiss Tuesday, May 8, 2012 9:24 PM
- Proposed as answer by Joel Weiss Wednesday, May 9, 2012 4:01 PM
- Unproposed as answer by Joel Weiss Wednesday, May 9, 2012 4:01 PM
Tuesday, May 8, 2012 9:23 PM