Home > Mobile >  Is it possible to have "sub-models" with Entity Framework?
Is it possible to have "sub-models" with Entity Framework?

Time:05-18

I was wondering if it was possible to have "sub-models" within Entity Framework entity models?

For example, this:

public class User
{
   public int Id { get; set; }

   public UserPermissions Permissions { get; set; }
}

public class UserPermissions
{
   public bool CanRead { get; set; }

   public bool CanWrite { get; set; }
}

Then in the actual table, there would be these columns:

Id
CanRead
CanWrite

or even with a prefix, like that:

Id
Perm_CanRead
Perm_CanWrite

Is that a thing? Is there an alternative you may know of? Or is my best bet to use a base/abstract class like public class User : UserPermissions { }?

CodePudding user response:

Yes, this is possible in EF Core by using owned entities. The enter image description here

  • Related