Entity Framework Code First: Custom Mapping Using

2019-02-26 09:54发布

问题:

In ScottGu’s article, he uses OnModelCreating method to define schema mapping. I am wondering if Entity Framework has some existing feature that allows us to do the mapping through attribute. For example, the Dinner class mentioned in that article could be like:

[MapTable="tblDinner"]
public class Dinner{
  [MapColumn="colId"]
  public int DinnerID {get;set;}
}

回答1:

Absolutely. See 'Table' and 'Column' attributes in: http://msdn.microsoft.com/en-us/data/gg193958



回答2:

I think you are looking for 'Data Annotations' Check the following blog for more info: Data Annotations in the Entity Framework and Code First

They allow you to place attributes in your source code that will tell the Entity Framework how to do the mapping