I have a database with the constraint that all primary keys end with _Id
. For my EF classes, I wanted to omit the underscore so I changed the T4 template for the generation of properties to this ( CodeStringGenerator.Property
):
public string Property(EdmProperty edmProperty)
{
var dbName = _code.Escape(edmProperty);
var propResult = "";
if(dbName.Contains("_"))
{
propResult = "[Column(\"" + dbName + "\")]\r\n\t";
dbName = dbName.Replace("_", "");
}
return propResult + string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}get; {4}set; }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
dbName,
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
This removes all "_" from the property name and writes a column attribute to the property. But it seems like this breaks EF. With the column attribute, EF throws an exception every time I try to make anything like selecting an entity.
The exception is:
The entity type ENTITY is not part of the model for the current context.
If I write the original name like in the database it EF will work with the entity but not if I add the column attribute.
Is there any way to rename all properties or make the column attribute work? Using EF6.2
Afters some days of researching i did not found a way other then using AutoMapper for OneWay transformation of Column to Entity.Property names so i decided to switch my ORM to a simpler one that works Primary with the Database First approach.
Conclusion: Not Possible
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.