❔ [EF Core] Ignore properties from constructor of base class

Hey, I am currently trying to implement a new model in EF Core which looks like this:
internal class PlayerV : Player
{
public bool LoggedIn { get; set; }

public PlayerV(ICore Core, IntPtr NativePointer, ushort Id) : base(Core, NativePointer, Id)
{
LoggedIn = false;
}
}
internal class PlayerV : Player
{
public bool LoggedIn { get; set; }

public PlayerV(ICore Core, IntPtr NativePointer, ushort Id) : base(Core, NativePointer, Id)
{
LoggedIn = false;
}
}
In the current implementation, I only want to save the LoggedIn property to the database, because I don't need the other ones from the base class. They're just needed for the business logic in the code. So I tried to ignore them with the ModelBuilder:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PlayerV>()
.Ignore(x => x.Core)
.Ignore(x => x.NativePointer)
.Ignore(x => x.Id);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PlayerV>()
.Ignore(x => x.Core)
.Ignore(x => x.NativePointer)
.Ignore(x => x.Id);
}
But I am still getting the error message:
No suitable constructor was found for entity type 'PlayerV'. The following constructors had parameters that could not be bound to properties of the entity type:
Cannot bind 'Core', 'NativePointer', 'Id' in 'PlayerV(ICore Core, IntPtr NativePointer, ushort Id)'
Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound.
No suitable constructor was found for entity type 'PlayerV'. The following constructors had parameters that could not be bound to properties of the entity type:
Cannot bind 'Core', 'NativePointer', 'Id' in 'PlayerV(ICore Core, IntPtr NativePointer, ushort Id)'
Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound.
That kinda makes sense to me. But I really need to find a way how just to ignore the properties from the base class, I don't want an additional database wrapper class or something. Is that possible?
16 Replies
jcotton42
jcotton422y ago
the issue is the constructor how is EF supposed to construct your model when it doesn't have values for any of the parameters? the properties aren't the issue
Angius
Angius2y ago
EF models should, ideally, not have a custom constructor
jcotton42
jcotton422y ago
though, having a model like that (nearly all the props ignored) is really smelly
Angius
Angius2y ago
And instead of .Ignore()ing a bunch of properties... just make a model that doesn't have them Exactly
jcotton42
jcotton422y ago
your db models should be db models and nothing else also, even if what you currently had worked, that table has no primary key
𝐃𝐞𝐫𝐢𝐬𝐨𝐧
okay, thanks for the tips guys. so the better approach would be to create an extra e.g. PlayerDB class, which can be constructed from PlayerV which just handles the database stuff?
jcotton42
jcotton422y ago
yes and it also needs a primary key some sort of unique ID
Angius
Angius2y ago
Unless the Player class you inherit from has it
𝐃𝐞𝐫𝐢𝐬𝐨𝐧
already prepared that for the PlayerDB awesome
Id = Guid.NewGuid().ToString();
Id = Guid.NewGuid().ToString();
sadly, the Player class isn't even meant to be a database model. i just hoped i could make it fit for me, you know
Angius
Angius2y ago
Let the db generate the PK Far as what is made to be in the db and what isn't meant to... EF models model the database
jcotton42
jcotton422y ago
don't try to be clever with your db models it just ends in pain
Angius
Angius2y ago
They should contain all that you need stored there And only what you need stored there .Select() into other classes if need be Or use an automapper Or something else
jcotton42
jcotton422y ago
ugh please no
Angius
Angius2y ago
But EF models should just describe the database
𝐃𝐞𝐫𝐢𝐬𝐨𝐧
okay i see, i'll keep that in mind. thank you guys so far
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server