logo
down
shadow

Using Entity Framework constructors in derived classes


Using Entity Framework constructors in derived classes

By : user2955441
Date : November 22 2020, 10:40 AM
wish of those help Probably because EF can't differentiate between items it added vs those you added. As it adds each item, it probably starts by just making sure the List != null and doesn't create a new List. This allows for some flexibility/control in deriving the List. If you wanted to use some specialized List, but EF always began be create a new empty list before adding items, then it'd blow away your attempt to use some derived List or different type implementing IList/ICollection. I realize that's not what you're trying to do, but explaining why they might have chosen not to begin by clearing your list.
A bit of a hack, but you can create a constructor overload that takes a bogus parameter, since EF will use the default constructor. Do no default initialization in the default constructor. The overload you call explicitly when you want to generate a new entity and it will do the default initialization. If you need to do this up the inheritance hierarchy, then that constructor could use the base(someParam) syntax to call up to the parent class as well.
code :


Share : facebook icon twitter icon
How to query an Entity Framework entities container for objects of derived entity classes?

How to query an Entity Framework entities container for objects of derived entity classes?


By : user1976367
Date : March 29 2020, 07:55 AM
will help you I have a complex inheritance structure in my data model. A majority of classes in my model (which is designed in VS 2010 with a DB generated by it after) are derived from 3 basic classes. And only these 3 classes can be found among the context members. How do I use all the derived classes at all? , You can query for subtypes like this:
code :
  var horses = from animal in ctx.AnimalSet
               where animal is Horse
               select animal as Horse;
var horses = from animal in ctx.AnimalSet
             where animal is Horse //edit, this line is not needed
             let horse = animal as Horse
             where horse.TracksWon > 3
             select horse;
Entity Framework Table Per Class/Type, Many To Many relationship between derived classes

Entity Framework Table Per Class/Type, Many To Many relationship between derived classes


By : Shaz
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I have the following class hierarchy: , This is now solved. It was a foreign-key mapping issue:
code :
                modelBuilder.Entity<User>().HasMany<Role>(r => r.Roles).WithMany(u => u.Users);
Filter with multiple derived classes with Code-first Entity Framework

Filter with multiple derived classes with Code-first Entity Framework


By : Saptarshi Choudhury
Date : March 29 2020, 07:55 AM
may help you . Lets say I have a User with 2 derived entities Student, Teacher. I used the TPH method, so I actually have no property at all in my classes to tell me who is a teacher or not. , Try this one. It works at least for EF 5.
Entity Framework 5 - Enum based Discriminator for derived classes

Entity Framework 5 - Enum based Discriminator for derived classes


By : Serj
Date : March 29 2020, 07:55 AM
help you fix your problem As far as I know you cannot do that. Doing the explicit Requires to specify the disciminator is only to give it a name - not to connect it to your property.
As far as I know that always results in that error (later) that you're describing. If you want to specify discriminator it has to be 'automatic'one (at least I never managed to define it that way)
Entity Framework Core - More than one Discriminator for derived classes

Entity Framework Core - More than one Discriminator for derived classes


By : K.Baranowski
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Well, seems a little odd to need two discriminators -
but you could structure it so that Activity has a discriminator and then the sub classes also have their own discriminators
code :
modelBuilder.Entity<Activity>()
.HasDiscriminator(x=> new {x.ActivityStatus, x.ActivityType})
.HasValue<PlannedDowntimeActivity>(new {ActivityStatus.Planned,ActivityType.Downtime} );
Related Posts Related Posts :
  • Exception after installing ASP.NET Route Debugger
  • Web API httpget with many parameters
  • How to hard code a select list from a value?
  • Passing parameter to telerik popup
  • ASP.NET Web API with x509 security certificate
  • ASP.NET get cell from DataRow in a datatable?
  • how can I use a Microsoft Account to authenticate to my website
  • Unable to start debugging on the web server. Unable to connect to the webserver
  • Not able to connect to cube
  • PDF creation using itextsharp is not working in client's server
  • Date field does not display the value in Chrome browser
  • Get Text of Hyperlink which is sitting inside template field in a gridview
  • how to scale image in email body?
  • FormAuthentication not working
  • Code first database not being created from model
  • Loading external DLL into WinDBG
  • Pass JSON data to strongly typed partial view in ASP.NET
  • ASP.NET MVC 4 Application w/ Areas apps suffering horrible render times
  • view sections cause problems if not rendered in layout
  • Modifying code in a website on IIS
  • Autofac and WebAPI - Default constructor error
  • RouteConfig triggers 500 error when refreshing page
  • Is it possible to change the query string value
  • MetroUI buttoon server side Click Event
  • Change default idseparator in .Net 3.5?
  • Placing DataBound Drop Down List into TableCell
  • MVC 5 AspNet Identity - Value cannot be null. Parameter name: source
  • Save the value of Dynamically created textbox
  • asp.net identity membership saving both email and username for a user
  • IIS Manager Error
  • Grid dont have data Kendo UI and AngularJS in ASP.NET MVC
  • Project stops working after installing signalr
  • While querying with linq , Max function retrieves upto '9' if more than 9 values in the list
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org