Inheritance of database tables in VS - c#

I want to create Table-Per-Type database table inheritance.
Simply base table RowElement will have 2 children tables. First child table Lyrics will inherit all parents RowElement's columns and will just add one more column. Second child table ChordUse will only many-to-one relationship to table Chord.
This is how my database schema looks like:
alt text http://www.freeimagehosting.net/uploads/ae4d8bd348.jpg
The problem is that when I let the VS create E/R diagram for me it creates this:
alt text http://www.freeimagehosting.net/uploads/774b194451.jpg
So it creates an 1-to-1-0 entity relationship instead of inheritance. I can't get VS to create inheritance relationship mapped correctly to database. Please help me.
After generating E/R diagram from my database I would like to have something like:
alt text http://www.freeimagehosting.net/uploads/e532f09b8a.jpg
Where RowElement is abstract class. Thank you for any help.

The EF designer (for good reasons, IMHO -- inheritance is overused in OR models) will never infer inheritance. You need to set it up yourself. You are asking for "table per type" inheritance. Follow the walkthrough with the demo model. When you've got that working, you'll be ready to do it with your real model.

Related

Database First Entity Framework multiplicity

I am working on a simple database and to be specific here is the model generated by database first approach (Visual Studio 2017 Community, Entity Framework 6.2):
Generated Database Model
I'd like the UserMessage table to be able to point to itself with a field named AnswerId, this is a nullable foreign key referencing its primary key. Again, to be specific, here is the part where I create the table:
UserMessage table script
My problem is that when Entity Framework generates the classes based on the existing database everything goes fine except for this particular table, in which EF suggests (I don't know why) that the UserMessage table has a multiplicity of 0..1 - * to itself while it should 1 - 0..1 (because a message may have a direct answer, but not more than 1, though that message, which is the answer, could also have an answer, so it's just like a linked list).
Here is the generated class: UserMessage generated class
To sum up the whole thing: I'd like to know why Entity Framework generates my class the way it does, and how could I make it generate it so that I only have a virtual property pointing to the answer (in case it has one), but not a collection.
Thank you for your answers!
I think what you're seeing is a correct interpretation by EntityFramework.
UserMessage1 represents a collection of all the UserMessages that have references to the parent as their answer. I understand that you probably won't use that collection for anything but it's not wrong that it's there. UserMessage2 seems to be the property you're looking for. Maybe you could rename those properties in the diagram so they're not confusing.
UserMessages1 = MessagesThatReferenceMe
UserMessages2 = the Message that I may or may not reference
I don't see how you can stop EF from generating this collection. I think if you delete the property in the diagram you will have to delete it every time you update the diagram.
Maybe try deleting the 2nd UserMessage navigation property in your model.

unwanted column in entityframework database

I am working on an MVC project in Visual Studio Ultimate 2013 and I have some unwanted columns in some of my tables and I cannot really understand why is it so.
In table Categories there is the column Template_TemplateId which is probably because in the Template entity class there is a ICollection<Category> property.
I want a Template to have many categories, however a one category can be repeatedly used by many Templates so I guess the Template_TemplateId column is something I don't want there.
Can someone explain, why is this so, and what should be a right approach to have the model the way I want it?
Thank you.
Model First:
Be default, EntityFramework creates one-to-many type relationships. When you create those, you need a foreign key in the "one" side of the relationship.
This is the "Template_TemplateID" field. It is the foreign key into the "Templates" category. However, you said you wanted a many-to-many relationship, so you are absolutely right; that field is useless!
You need to specify a cardinality of "*" on both ends of the relationship, then the model will create an intermediate table allowing the many-to-many relationship. The foreign key should go away at this point.
Code First:
Same problem, but the solution is to make a collection of "Template" in categories and and a collection of "Category" in templates, thus creating the many-to-many relationship.
If you want a many-to-many relationship between Templates and Categories create two collections, on on each class - on Template, create a ICollection and on Category, create ICollection.

Entity Framework: subset entity of larger entity

Sooo basically I have a table called Comment. On that table I have three fields,
ID
Title
Text
I've created an Entity object for the table already and it maps all three fields, but what I want now is another Entity called CommentHeader that will map only ID and Title. I want to only load the titles of all the comments and not the text for speed reasons. So what's the best way for going about this?
I'm not looking for a Select statement with a var object. I can figure that one out on my own and I really don't like that solution because I'd much rather abstract it behind an Entity object.
I've tried the obvious solution, which was to just copy the original Entity object and delete Text from it. That resulted in an error because only one Entity can map to one table without conditions. It sounds to me like I have no choice but to use a Select statement. I just wanted to make sure before I did something stupid.
(By the way this example only has three fields for simplicity's sake. Assume that the header could have considerably more fields in it. This is the primary reason I don't want to just use a select with a var object, because it's not just one field but could be a whole bunch of fields).
The easiest way probably would be to create a view ("CommentHeaders") in the database that only selects ID and title from the Comment table. Then update your model and add the view, which will create a new entity based on those columns.

LINQ many-to-many relationships: Solution?

LINQ so far has been remarkably elegant, but to perform basic m2m queries it offers no solution I can imediately see.
What's worse is that while it works for any other table relationship, LINQ is not giving me an association in the class structure for my m2m table.
So I can do things like
artwork.artists.where(...)
//or
artist.Artworks.add(artwork)
but I can't do
artwork.artowrks_subjects.tagSubjects.where(...)
//or
tagSubject.artworks_subjects.add(artwork)
alt text http://img299.imageshack.us/img299/257/20090902122107.png
Is there a common pattern for solving this limitation?
the way I have gotten M2M working in LINQ2SQL:
drag the tables into the builder,
like you're showing in the question
remove the relationship between
artworks_subject and artwork
create a new relationship FROM
artworks_subject TO artwork
click on the new relationship to get
its properties
change the
cardinality from OneToMany to
OneToOne (because ManyToOne doesn't
exist)
open the Child Property
section and change the Name field to
make it singular (Artworks to
Artwork)
now the tagSubject entity will have a collection of artwork_subjects, and the artwork_subject will have a property of type artwork called Artwork. so you can now make a LINQ expression like
var x = dbcontext.tagSubjects.Single(s=>s.name=="Landscape").
Artwork_Subjects.
Select(as=>as.Artwork.Name);
Found the solution myself. For automated relationships to work, both tables need primary keys (oops). Notice artworks_subjects is missing the PK symbol.
This largely depends on what framework you are using. It sounds like you are using LINQ-to-SQL, which is very literal about tables to objects. With Entity Framework, there is inbuilt support for many-to-many, in particular for the trivial case you've listed (a linking table with no additional properties). EF will generally spot this pattern, and hide the link table from the conceptual model (I can't remember, but it might need a spanning PK over the two FK columns).
Of course, this then goes sour if you want to add columns to the link-table; so in some ways I'd be tempted to leave it "as is".
With regards the where etc - how do you mean? You can do joins over the association, and you should be able to use Any etc; do you have a concrete example of what you want to do?
Yes. Instead of many-to-many use two many-to-one relationships:
Subject -*----1- ArtworkSubjectParticipation -1----*- Artwork

Same table relationship in Entity Framework

Is it possible to have an association mapping a table to itself?
e.g.
Table:
ConditionId
ConditionName
...
...
ParentConditionId
where we can have many ParentConditionIds each mapping to the same ConditionId.
I've tried a one to many mapping but I'm getting an error when there is no children.
Yes it is possible. Put the FK in your database and the wizard will map it correctly. Make sure ParentConditionId is nullable.
Take a look tutorial which can be download here . It explains in detail how to model self-reference table.

Categories