开发者

Cannot inherit custom types properties in Code-First

开发者 https://www.devze.com 2023-03-23 06:42 出处:网络
I\'m working on a new entities model creation. I identified several repeated properties(columns) which naturally needs to be inherited, like Id, CreatedBy, CreatedAt etc.

I'm working on a new entities model creation.

I identified several repeated properties(columns) which naturally needs to be inherited, like Id, CreatedBy, CreatedAt etc.

Now, I created a base class for this issue and it all works OK until I'm defining custom types properties in the base class, for example: public User CreatedBy{get;set;}

when running the application an EngineExecutionException is thrown.

If I'm omitting the public User CreatedBy{get; set;} and leave only the int, string, DateTime properties, it's fine and I can 开发者_运维知识库see it reflected in the DB creation.

What is my mistake?


I think your property should be virtual, which means that this property is a foreign key.


If UserID is a key to the User table (say an int), then you would have

public int UserID { get; set; }
public virtual User CreatedBy { get; set; }

The UserID property is a direct reference to the field in the table. The CreatedBy property is a reference to the User object that would be a representation of the User table. As mentioned elsewhere, the virtual keyword is used to make the User object lazy-load.

The UserID property matches the primary key on the User table and object. There are ways to match them up if they're not named the same, but it's easiest if they are. If you can give more info for CreatedBy such as type, how it's represented in the database, etc. I can give a more specific answer to your setup.

*edited for formatting.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号