table-per-hierarchy

EF6 - TPH foreign key mapping in derived classes using base class property

别等时光非礼了梦想. 提交于 2019-12-19 21:46:22
问题 I am using Entity Framework 6.0.2 with an existing database in which tags are stored in a single table that looks like this: Id : int, primary key TagType : string, determine the type of tag, either "usertag" or "movietag" ItemId : int, contains the Id of the item to which is referred (either a User Id or a Movie Id) The following classes describe this situation: public class User { public int Id { get; set; } } public class Movie { public int Id { get; set; } } public abstract class Tag {

how to make tablePerHierarchy false and use discriminator?

这一生的挚爱 提交于 2019-12-13 05:02:46
问题 A question like what I need is asked here but it's answer is not satisfying what I need. I have classes like this : abstract class A{ String a } class B extends A{ String b } class C extends A{ String c } I want to have a table for each class, so I add tablePerHierarchy and set it false like this: abstract class A{ String a static mapping = { tablePerHierarchy false } } but when I run the program and it creates a database there are no discriminator, and I want to query table A and find out if

Entity Framework Table Per Hierarchy Inserting Multiple Id Columns

点点圈 提交于 2019-12-11 12:36:16
问题 I have seriously spent two work days trying to a TPH setup from Database First to Code first. The Error I get is Something like "Invalid Column Name Entity_EntityId/ Entity_Entity_Id1" I've drawn up a very basic reproduction of the issue like so: internal class Program { private static void Main(string[] args) { using (var context = new Context()) { var baseClass = new Base {Name = "Test"}; context.BaseClasses.Add(baseClass); context.SaveChanges(); var baseClasses = context.BaseClasses.ToList

Issue with many-to-many relationship + TPH inhertitance in Entity Framework 6

坚强是说给别人听的谎言 提交于 2019-12-11 09:09:29
问题 I am running into an issue with EF6, though I'm fairly sure that this applies to previous versions that support this type of mapping. I fear I know the answer to the question at hand, but I hope that I am doing something wrong, or there is a better workaround than what I present here. All classes are gutted for clarity. So I have public abstract class SoftwareFirmware { public long Id { get; private set; } public ICollection<DeviceType> DeviceTypes { get; private set; } public

EF: select all entities of subclass (inheritance)

梦想与她 提交于 2019-12-11 07:16:36
问题 I am using the Database first approach and DbContext. I have several inheritance structures (TPH) in my data model. But DbContext only creates one DbSet for the base class, and none for the subclasses. How should I retrieve all Entities of a specified subclass? I cannot write queries based on the mapping criteria, as i had to remove those fields from the data model. Can I simply add a new DbSet to the Entities class (partial class) ? 回答1: You can add new properties returning DbSet s of your

Entity Framework Code First TPH inheritance - can different child classes share a field?

我们两清 提交于 2019-12-10 15:57:09
问题 I have an Entity Framework Model created using Entity Framework Code First that is using Table Per Hierarchy inheritance where the structure looks a little like this: public abstract class BaseState { public int Id { get; set; } public string StateName { get; set; } // etcetera } public class CreatedState : BaseState { public User Owner { get; set; } } public class UpdatedState : BaseState { public User Owner { get; set; } } Now what that creates is in my BaseStates table I have Owner_Id and

Multiple Inheritance with Entity Framework with TPH

*爱你&永不变心* 提交于 2019-12-09 07:56:27
Further to this question: Entity Framework TPH with multiple abstract inheritance and VS.2008 sp1 .net 3.5 c# I decided to add Organizations and a School. Organization(abstract) inherits from Party, and School(concrete) inherits from Organization. I get the error: Error 1 Error 3034: Problem in Mapping Fragments starting at lines 73, 93: Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with different keys to the same group of rows. C:\Users\xxx\Documents\Visual Studio 2008\Projects\TEST\TEST\Model1.edmx 74 29 TEST

Entity Framework TPH with multiple abstract inheritance

£可爱£侵袭症+ 提交于 2019-12-08 04:50:23
问题 I'm trying to do a Table Per Hierarchy model in Entity Framework(VS 2008 sp1, 3.5). Most of my models have been very simple, an abstract type with multiple sub-types that inherit from it. However, I've been struggling with this latest challenge. I have STUDENTS that I'd like to inherit from PERSONS(abstract) that should inherity from PARTIES(abstract). Every time I do this I get a "Error 2078: The EntityType 'Model.PERSONS' is Abstract and can be mapped only using IsTypeOf." I guess the

How to change a Domain Class model object from one derived class to another in Grails/GORM

筅森魡賤 提交于 2019-12-08 01:59:24
问题 Given the following Grails GORM Domain Classes and using table-per-hierarchy inheritance: class Book { static belongsTo = [ parent: Parent ] String title } abstract class Parent { static hasMany = [ books: Book ] } class A extends Parent { String asset } class B extends Parent { String asset } Say I have retrieved an instance of class A from the database. I want to convert it to an instance of class B. What is the grails idiomatic way to do this? Without the hasMany relation, I would just

EF4.1 Exception creating Database with table-per-hierarchy inheritance

丶灬走出姿态 提交于 2019-12-06 07:18:10
问题 I have created a very simple project to demonstrate table-per-hierarchy inhertiance. In my unit test which tries to generate the database, i get one of a number of errors depending on the config: without the Required() method Map<InActiveUser>(x => x.Requires("IsActive").HasValue(false)); Map<ActiveUser>(x => x.Requires("IsActive").HasValue(true)); delivers: System.Data.DataException : An exception occurred while initializing the database. See the InnerException for details. ----> System.Data