I'm trying to create an MVC project using Datbase First Modelling in Entity Framework. The issue I have is when I have selected the tables I wish to use, the models are created but relationships are not generated. I have read somewhere that EF does not work well with composite keys which unfortunately a number of my tables are made up of.
I tried adding the associations myself through the designer but because my table has a composite key and its mapping to a table on the first portion of the composite key to a column on another table I get an error. This is because it insists on having a mapping for the second item of the composite key
e.g.
Table 1 Table 2
D_ID A_ID
FC SomeField
Description SomeField
etc... D_ID
I need to map Table 1 and 2 based on D_ID but Table 1 is made up of a composite Key of D_ID & FC
Using database first how do I get around this?
Thanks
Related
I know how to generate entity objects using database first approch using SQL TABLES. However I am struggling to create a hierarchical entites from a SQL VIEW. This is because I am unable to assign a primary key and foreign key to the id and parent fields ( see below)
An example SQL VIEW is as follows:
id, name, parent
1, John, null
2, Bill, 1
3, Jane, 2
4, Bob, 2
The underlying view is made of joins and unions hence I cannot use a SQL TABLE
There appears to be other examples of code first approaches but I'm looking for a solution that can use my existing view.
I'm using database-first approach with Oracle. One of my table doesn't have a primary key. It only has 2 columns which are foreign keys of other tables.
I have generated model in ASP.NET MVC project from database (Add - New Item - ADO.NET Entity Data Model).
But there is a problem - I get an error:
Error 159: EntityType 'DbModel.Store.SomeTableWithoutPK' has no key defined. Define the key for this EntityType. E:\Git_repo\ZZ\ZZ.Domain\DAL\DbModel.edmx
Does this mean that each table must have a primary key? Can I avoid this? Or I will be forced to add new column with a primary key to this table? Of course there is also possibility to apply primary key to multiple column, but is it necessary?
Every table should have a primary key for database efficiency and so that you can edit records.
You don't need to create a new column for the primary key in your 2 column table
In designer, select both columns and use both together as the primary key. As long as nulls are not allowed and there are no duplicates you should be OK.
Since this is a many to many table and you are using EF, you may find later that adding a datetime column to the table with getdate() as the default value will make data maintenance easier
Before I submit my question, please be aware that I'm working with an existing database owned by a third party vendor, so unfortunately changing the database format is not an option.
Here's the issue: I have an Entity mapped to a database table that has a varchar column that contains one to many foreign keys in csv format. Those foreign keys correspond to the ID's of another Entity type. What I've been doing is writing a function that creates a List of ID's from that csv list and then I search for that Entity through the DBContect object. What I'd like to do is map a relationship between the entities. Is there a way to do that? Thanks!
Unfortunately there is no way to do that without changes in the database. EF is ORM tool but it is still very dependent on correctness of database design. Storing multiple values in single column is breaking even first database normal form. For EF your column containing csv data is single string value and you cannot make relation on that value.
Btw. it is even more complicated because the column cannot represent one-to-many relation in standard relational meaning - that would require dependent entities to contain Id of your master entity, not that master entity contains Ids of all dependent entities.
Suppose I have 3 tables in DB for a many to many relationship:
TabA(id1, ...): Primary Key: id1
TabB(id2, ...): Primary Key: id2
TabAB(Id1, id2,..): Foreign Key: id1, id2
So when create edmx with VS 2010 from DB, I only get two entities TabA and TabB in the model because TabAB has no primary key.
How to process this case with EF?
Are you sure EF didn't just turn TabAB into a relationship? It won't appear as a table in the model if there are no other columns. EF figures out that TabAB is a join table and treats it accordingly.
If not, the best way would be to alter TabAB to have a compound primary key of both id1 and id2. If there is some reason that combination of values is non-unique, it might be good to examine why.
The common way is to handle many-to-many relationships in EF - to have three tables in storage and only two tables in conceptual model. Third table in storage contains of columns that represent the foreign keys referencing to the main tables, and the primary key of the intermediate table is built over these reference columns. Designer just hides it :)
Read more here - http://weblogs.asp.net/zeeshanhirani/archive/2008/08/21/many-to-many-mappings-in-entity-framework.aspx.
I am just getting started with Microsoft's Entity Framework, using it for an MVC project since MS seems to be really pushing it, and I'm running into some issues. In my database there are multiple lookup tables tied to a single table via foreign keys. Within the entity framework I am trying to combine these into one so that I have a simplified single view for this data in my model. However, this doesn't seem possible from the designer view. Is there something obvious I'm missing? Is there a way that I can edit the edmx file manually to produce this sort of model?
At the moment, Foreign keys and lookup tables in Entity Framework are a PAIN.
EF with LINQ makes getting your data super-easy, and on the surface it looks easy to update, but with lookup tables things get difficult (for now... read on...)
I'm not sure how you would "combine" your lookup tables into a single table. If each table contains a different type of "lookup entity" then IMHO they should be represented separately in your EDM. I'm guessing you're having headaches updating a record's foreign keys to the lookup tables. That's because it is a headache.
Changing foreign key values:
MyDBEntities _db = new MyDBEntities();
//get a Person
MyDBEntities.Person person = (from p in _db.Persons
where p.Id = 1
select p).First();
// This sets the foreign key value in the Person table on the PersonType field
person.PersonTypeReference = new EntityKey("MyDBEntities.PersonType", "PersonTypeId", 3)
The next release version of the Entity Framework will have a new concept called "FK Associations." This will bring back the sanity of setting the foreign key value directly rather than having to create and set an EntityKey.
HTH.