I'm trying to justify using EntityFramework and falling at the very first hurdle. I'm using the database first approach. Here is what it looks like in SQL Server 2008 R2:
As you can see, VERY simple domain. The association is a one-to-many relationship between User.Id and Blog.UserId.
However, even with this incredibly simple domain, EF fails. When I import the tables, I get this:
As you can see, there is no association. The relationship is gone and no matter how many times I delete, regenerate, it will not appear. "Include foreign key columns in the model" is checked.
However, if I create the tables based with keys based on ints instead of GUIDs, the relationships are created.
Is this a bug, a limitation or my own stupidity?
It doesn't inspire my confidence. How on earth will it perform in a real-life scenario, with a hundred tables and a thousand associations?
VS 2012
SQL Server 2008 R2
EF 5
I've resolved the issue. The problem was caused by a rogue index I did not know I had on the table.
If there is a UNIQUE index on the same field as the primary key, this will cause EF to fail to generate the PK-FK relationship at all.
Simply removing the unnecessary unique index (primary enforces unique anyway), the EF designer immediately picked up the relationship.
Related
When our IT department converts Access databases to SQL Server the relationships do not transfer over. In the past, I have provided ERDs that they can use to build the relationships. In this case, I didn't.
What are the possible consequences of defining the table relationships in the MS Access Front End versus on the SQL Server itself?
It would be ideal if I could just create the relationships in Access and avoid submitting a request to IT, but I don't want to risk performance issues now or in the future.
There may be some misconceptions.
A relationship in SQL Server enforces referential integrity (an order cannot have a customer ID that doesn't exist). It does not automatically create an index on the Foreign Key, so it has per se no impact on performance.
But in most cases it is a good idea to define an index on a foreign key, to improve performance.
A relationship that you define in Access on linked tables does neither. It cannot enforce referential integrity (that's the server's job).
It is merely a "hint" that the tables are related via the specified fields, e.g., so that the Query Builder can automatically join the tables if they are added to the query design. (copied from here)
So you should
Create the relationships in SQL Server to avoid inconsistent data. ("But my application logic prevents that!", I hear you say. Well, applications have bugs.)
Create indexes on foreign keys where appropriate to avoid performance problems.
If you are working with queries in the Access frontend, additionally define the relationships there.
Ideally you should have a test server where you can yourself define the relationships, and just send the finished SQL script to IT.
I am using MVC 3 with EF. I have created a entitymodel from SQL Server db and some of the tables are not showing in diagram and in code in intellisence. I have created the model again but same problem. Those tables having many to many relationship not appearing in diagram or entity list, shown in table store. At the same time few other tables with many to many relationships are visible. What is missing here? I have check PK and FK all seems correct. SQL Server also shows them in diagram but not in Visual Studio Entity model diagram.
It is something like
Department
DeptID
(other fields)
Phones
PhoneID
Phone
Has_Phone (this is many to many) and not showing up in entity model
PhoneID
DeptID
How can I get all the phones using LINQ for a departmentID?
Please help Thanks QF
I got the solution.
If the table has only two fields, the primary keys from two other tables then EF behaves like that. The solution is to create just one more column in MNM table and update the model from database. And finally the solution will be resolved itself.
If someone knows any other better solution then please share with us.
I'm fairly new to Vs Data capabilities, and this is my first data generation plan. I have implemented a database using a Vs2010 database project, and used it to deploy to a sql server express 2008 database. All the tables use identity columns as their primary keys, and they're related to one another with foreign keys.
I set up a data generation plan, but when I try to generate data with it, the tables are simply populated in alphabetical order, which is of course going to fail. The only tables that populate correctly are the lookup tables and other sorts of independent entities with no FK constraints. The rest are skipped after the first table fails.
Supposedly the generation plan determines the population order based on FK dependencies. What happened?
edit: someone with the rep for it should make a visual-studio-data-tools tag, since DBPro is no longer (nor really ever was) a product name.
So apparently according to this thread the data generation plan blows up when you have a table containing only a primary key and no other columns. It turns out that one of my independent entities, whose only purpose is to serve as a joinder to one of my other tables, fit this description. After adding a harmless Description column, I was able to proceed fixing other problems until the generation plan completed successfully.
I am working on an old database, that i do not want to touch or modify in any way if possible.
I want to build a new application that uses it's data but the database has no actual relations despite having primary and foreign keys linking tables.
If i import these tables into an Entity Framework model and add the associations manually, will i be able to use things such as lazy loading and linq?
Many thanks,
Kohan
This is definitely possible. Entity Framework simply generates SQL queries containing joins or where clauses that reference columns that you define in your conceptual model as foreign keys. The generated SQL is directly executed by the database.
Primary and foreign keys are only in your database for referential integrity. As a very simple test you can execute a SQL statement directly in your database that joins two related tables that do not have a foreign key relationship. You'll see that the query simply works. Entity Framework does exactly the same when you correctly define the relationships in your model.
I'm in the process of designing a fairly complex system. One of our primary concerns is supporting SQL Server peer-to-peer replication. The idea is to support several geographically separated nodes.
A secondary concern has been using a modern ORM in the middle tier. Our first choice has always been Entity Framework, mainly because the developers like to work with it. (They love the LiNQ support.)
So here's the problem:
With peer-to-peer replication in mind, I settled on using uniqueidentifier with a default value of newsequentialid() for the primary key of every table. This seemed to provide a good balance between avoiding key collisions and reducing index fragmentation.
However, it turns out that the current version of Entity Framework has a very strange limitation: if an entity's key column is a uniqueidentifier (GUID) then it cannot be configured to use the default value (newsequentialid()) provided by the database. The application layer must generate the GUID and populate the key value.
So here's the debate:
abandon Entity Framework and use another ORM:
use NHibernate and give up LiNQ support
use linq2sql and give up future support (not to mention get bound to SQL Server on DB)
abandon GUIDs and go with another PK strategy
devise a method to generate sequential GUIDs (COMBs?) at the application layer
I'm leaning towards option 1 with linq2sql (my developers really like linq2[stuff]) and 3. That's mainly because I'm somewhat ignorant of alternate key strategies that support the replication scheme we're aiming for while also keeping things sane from a developer's perspective.
Any insight or opinion would be greatly appreciated.
I second Craig's suggestion - option 4.
You can always use the GUID column, populated by the middle-tier, as your PRIMARY KEY (that's a LOGICAL construct).
To avoid massive index (thus: table) fragmentation, use some other key (ideally an INT IDENTITY column) as the CLUSTERING KEY - that's a physical database construct, which CAN be separated from the primary key.
By default, the primary key is the clustering key - but that doesn't have to be that way. In fact, I improved performance and drastically lowered fragmentation by doing just that on a database I "inherited" - add a INT IDENTITY column and put the clustering key on that small, ever-increasing, never-changing INT - works like a charm!
Marc
Huh? I think your three options are a false choice. Consider option 4:
4) Use the Entity Framework with non-sequential, client-generated GUIDs.
The EF can't see DB-server-generated GUIDs for new rows inserted by the framework itself, sure, but you don't need to generate the GUIDs on the DB server. You can generate them on the client when you create your entity instances. The whole point of a GUID is it doesn't matter where you generate it. As for GUIDs generated by a replicated DB, the EF will see them just fine.
Your client-side GUIDs won't be sequential (use Guid.NewGuid()), but they will be world-wide, guaranteed unique.
We do this in shipping, production software with replication. It does work.
Another option (not available when this was posted) is to upgrade to EF 4, which supports server-generated GUIDs.
Why not use identity column? If you are doing merge replication you can have each system start at a separate seed and work in one direction (e.g. node a starts at 1 and adds 1, node b starts at 0 and subtracts one)...
You can use stored procedures if you are really stuck on using NewSequentialID(). You can bind the result columns from the procedure to the appropriate property and once inserted the SQL-generated GUID will be fed back into the object.
Unfortunately you have to define SPs for all three operations (insert, update, delete) even though the other operations would complete properly using the defaults. You also need to maintain the SP code and ensure it is synchronized with your EF model as you make changes, which may make this option unattractive on account of the additional overhead.
There is a step-by-step example at http://blogs.msdn.com/bags/archive/2009/03/12/entity-framework-modeling-action-stored-procedures.aspx which is pretty straight-forward.
use newseqid with your own orm (it not that hard) with linq