This is not a duplicate of this post although the title is very similar. I am using EF4 with MSSQL Express 2008 R2 on VS2010.
A simplified version of my schema is as follows:
Table [Team]:
Id (PK)
Member1
Member2
Table [Person]:
Id (PK)
FirstName
[Team].Member1 and [Team].Member2 are foreign keys pointing to [Person].Id.
When generating the .edmx via VS2010, the navigation properties under [Team] become "Person" and "Person1" despite giving distinct names to the FKs inside SQLServer.
Is it possible to force the .edmx generator to recognize my FK names in SQL Server? I'd like these names to be Member1Person and Member2Person, for example, so I don't have to manually rename them by hand. If not, what is the preferred way to redesign the tables/FKs to bypass this altogether? Thank you.
I have had a similar issue but I believe the answer to the question is you simply have to rename the Navagation property to what you want. The Entity Framwork designer will always keep you changes to the property names on the Conceptual side of things.
Related
Hi right I'm just creating the foriegn keys/relationships in sql server management studio, but I've come across a bit of a problem.
I have several tables lets call them
my_form1,
my_form2,
my_form3
When filling out any of these 'my_forms' I keep a record of that. In another table lets call that 'forms'
In forms I have these fields
form_id
form_type - I store the type of form (1,2,3)
myform_id - I store the id of the form.
How do I correctly show this on my sql server management relationship.
I've cut down the problem and there are obviously more fields but the situation is the same.
This kind of relationship can't be enforced with a simple foreign key, and therefore won't show up on a diagram generated with SSMS.
You can create your own diagram in Visio and draw it there. I believe this is what is called a "Subtype" entity relationship.
I've deploy my database through ADO.net Entity Data Model by executing its diagram output query against my pre-made online database.
My database contains Only Lightswitch supported relations, One-Many, ZeroOne-Many & One-ZeroOne.
Finally, Attaching my database to my Lightswitch application as an external database and I was shocked by its result.
Lightswitch converts ALL One-ZeroOne relations to One-Many relations!
I suspect everything, retry tens of times trying to figure this strange issue out with no good news!
Question:
Why Lightswitch converts ALL One-ZeroOne relations to One-Many relations? Inconsistency!
Unless this is a bug, it would depend on the nullablility of your PK/FK. LightSwitch will not allow 0-1 for any key property that is not nullable.
Finally, I've solved it by a brute-force-like trials :S
Solution : ( Please don't ask me a WHY question as IDK will be my answer )
After adding a One-to-ZeroOne Relation you might have one or two Navigation Properties added to associated tables, but
You MUST add a Foreign Key as Scalar Property to the ZeroOne Table with Same type & true Nullable properties.
You MUST adjust the Residential Constrain for the association by double clicking the association line itself and make the FK in the ZeroOne Table dependent on the One table Key.
Generate the database script, execute it and Don't Update LightSwitch data source, Delete data source & Attach it again instead as it won't update relations correctly!
I'm trying to use EF to model an existing SQL database. The DB is multi-tenant by having a clientID column in every single table (I cannot change this). I have table structure like the following.
Table 'ItemID' columns:
ClientID (pk)
ItemID (pk)
ItemName
Table 'Items' columns:
ClientID (PK)
ItemID (PK) [FK to ItemID.ItemID]
Version (PK)
ItemAttribute1
ItemAttribute2
ItemAttribute3
The DB is designed to store previous versions (rows) of the 'Item' object, hence the 'Version' column and PK.
I am new to the EF and trying to adopt it for my project. However, it seems that EF cannot handle this situation very well. I am open to all ideas including using stored procedures or views instead of access tables directly from EF. What about getting rid of the PKs and using 'independant' relations instead?
One specific problem I ran into is that EF (at least the designer) requires all PKs in one table be mapped to all PK columns in any related table. This does not make sense to me, as the example I've given will never work given that constraint. I am trying to make Items inherit from ItemID. The error I get is:
Error 3003: Problem in mapping
fragments starting at line 170:All the
key properties (ItemID.ClientID,
ItemID.ItemID) of the EntitySet ItemID
must be mapped to all the key
properties (Items.ClientID,
Items.ItemID, Items.Version) of table
Items.
I have been looking up all I can find on this topic and nothing has answered these questions for me. I appreciate any help. Thanks.
The error that you are getting from your EDM is coming from the fact that EF supports inheritance only if both entities have the exact same primary keys (hence a one to one relationship on database) so you cannot have inheritance between these 2 entities according to the current schema.
However, I don't see a problem on having a One to Many association between ItemID and Items entities and I believe this is the default EF behavior when you update your model from the database.
Please have a look at this post for more info:
Entity Framework, TPT Inheritance
Maybe i'm going about this wrong but my working on a database design for one of my projects.
I have an entity with a classification column which groups up entities into convenient categories for the user. These classifications are predefined and unchangeable by the user (at least thats the current design).
I'm trying to decide if I should have a 'EntityClassification' table which contains simply an 'Id' column as the primary key with no other information in order to have an enforced relationship between the Entity:Classification -> EntityClassification:Id.
I don't plan to have a name/description column in EntityClassification since my current thought is that I'll need to support localization of these pre-defined names which will be done with static string table like resource files downloaded to the client based on their country/language. There really isn't any other data which is associated with this EntityClassfication that I would want and a table seems like it might be an overkill?
Is this common/recommend practice for this type of problem? We're using SQL Server 2008 and don't have an enum datatype for the database which would seem to be really what i'm trying to achieve.
You should have the table with name and description not only for end user display, but internal documentation so when the users say 'my query based on this classification doesn't work!' someone hired in the future will know which ID they're talking about.
Do you just want to ensure that the values in Entity:Classification are restricted to your pre-determined list? If so a check constraint might be what you need.
Such constraints aren't as flexible as foreign keys: to alter the checked values we have to drop and recreate the constraint, but then you say there are no plans to change the values so that shouldn't matter.
I'm trying to do the following in the VS2008 Linq O/R designer, SQL 2005:
I have a table called Entity with an auto-incrementing primary key named PKey, and another field called Parent
I've made a View from this table, calling it vwEmployees, with a simple where clause.
Add both to O/R designer. in O/R, set the view's PKey to be PrimaryKey=true in properties in order to make the view updatable.
Added a one to many relation between them, from PKey in the table, to Parent in the view.
(A "parent" "entity" can have many "children" of the same record type)
Well, I get this error:
Incorrect AutoSync specification for member 'PKey'
I tried setting AutoSync to "Never", but still no dice.
If anyone has any clue as to why this occurs, I would greatly appreciate it.
I was able to create a Linq to SQL dbml file successfully using your examples using Visual Studio 2008:
I would check to make sure you have a correctly set the primary key in your Entity table database. It could also simply be a bug in the 2005 version of the designer that has now been resolved in 2008.