Add Unique Constraint in Entity Designe - entity-framework

I was wondering how to add a unique constraint while in the Entity Designer in Visual Studio 2015. All the answers that I've found only talk about adding a unique constraint in the generated C# code or in the generated sql.
Is there a way to do this in the designer itself?

Related

How can I map one custom Entity to some database tables in Entity Framework?

I have a database first model in my project. The 3 tables Document, DocumentItem and Product are imported from the database.
I want to create a new Entity named Order that joins some fields of these three database tables. I created that Entity as you see in the first picture and filled its table mapping as you see in the second picture.
After the build of the project I get the following errors:
Severity Code Description Project File Line Suppression State
Error Error 3025: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Product.Id) of table Product. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3025: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Document.Id) of table Document. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
Error Error 3024: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3024: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
I could not find the source of the problem. How can I solve these errors?
The error tells there's a foreign key missing in your project !
Considering that you know You cannot simply add tables from the database into your model and then create a new association in the model. By default it uses independent association which must be mapped to its database counterpart(e.g the relation must exists in the database as well). You must model your relation as FK association but it allows only one-to-one and one-to-many associations. In another words, when you add a new table with "Include foreign key columns in the model" checked in EF4 and the table doesn't contain any foreign key relationships then you try to add a association it will trigger this error. The solution is to Define Constraints in an EF4 model that don’t exist in the database.
Sometimes we have unwanted conflicts which can cause such this problems when you can delete the table from the EF designer and also delete everything related to that table manually (in the XML file), then add the table again and put the right mappings again.

Why is Entity Framework missing the last s in a table that ends with 'Status'

This is a really weird error, I have looked online and can not see any obvious reasons why this is happening.
I am using Database First Entity Framework version 5 and have added 4 tables called:
WorkStatus
JobStatus
SubJobStatus
SubJobStageStatus
JobStatus, SubJobStatus and SubJobStageStatus each has a foreign key constraint to WorkStatus.
I have updated my edmx to include these tables but for some reason each table is missing the last s from the word Status. So the tables and their navigation properties are now called:
WorkStatu
JobStatu
SubJobStatu
SubJobStageStatu
Does anyone know why this happens?
"-Status" is being interpreted as a plural, and Entity Framework is being "helpful" by de-pluralizing.
For example, if you have a table called "Customers" EF will generate an entity called "Customer", which is in fact nicer in code:
var customer = new Customer();
It isn't working out so well in your case; your tables are already singular.
You can rename your tables to "-Statuses" or update the names in the designer or disable pluralization:
Under Tools > Options

Entity Framework Cardinality Issue on a 0...1 Association

I have database tables that look like this:
A Task can be mapped to a Module, or not mapped at all (0...1). I'm using Entity Framework database-first, and when I generated the model from the database, the Task entity came through with Modules as a collection (0 or more). So I opened up my EDMX and changed the "Modules" navigation property on Task to 0...1.
Now, when I attempt to compile, I get this error:
Error 3003: Problem in mapping fragments starting at line 1241:Given the cardinality of Association End Member Task, it should be mapped to key columns of the table TaskModule. Either fix the mapping or change the multiplicity of this end.
I don't understand what I need to do to fix this. I've looked at the association details and can't see the issue. I know I'm probably missing something stupid, but am totally stuck. Association properties:
Visual Studio 2010 SP1, Entity Framework 4.3.1.0, SQL Server 2008 R2.
One way to do this is to redefine the primary key for the TaskModule table. Instead of the primary key being (TaskId, ModuleName) it needs to be just (TaskId). Then do an update model from database and change any of the associations manually that didn't get picked up from that update.
Well your database schema is not correct with the description you give :
the TaskModule table implicates a many-to-many relationship, not a many-to-oneOrZero.
In edmx, many-to-many relation tables are not displayed, but they still exist in database.
So you should fix your database, or be happy with the relation proposed by EF !

importing foreign key columns into model from visual studio 2008

I got a question about foreign key when "importing" the db into visual studio.
my development environment:
SQL 2005/2008/2008R2
Visual Studio 2008 ASP.NET MVC 2 with .Net framework 3.5 SP1
was following the MVC music store V1.0
in the tutorial pdf, page 50, it says when we creating the "Entity Data Model" at the last stage, we should select "Include foreign key columns in the model.
however, i am using VS2008 and when at this stage there is no such foreign key option to choose.
plz notice that inside the red circle, it should contain (AlbumId, GenreId, ArtistId, Title, Price, AlbumArtURL) as shown in tutotial
but my model is missing GenreId and ArtistId.
how to deal with it?
I encountered a problem later when the i creating edit form and need to use something like [[[[<%= Html.LabelFor(model => model.GenreId) %>]]]]] it is not working.
so i need the GenreId and ArtistId to appear in the Album. i guess it is about the foreign key problem...
do you know how to solve it?
Thanks a lot!!!
It is not possible. Importing foreign key columns is feature of EFv4 so you need .NET 4.0 and Visual Studio 2010 to use it.
In your case you must access FK value through EntityObject based API. Your entity should have property like GenreReference so you can use something like:
int genreId = (int)entity.GenreReference.EntityKey.EntityKeyValues[0];

Entity framework - "Problem in mapping fragments"-error. Help me understand the explanations of this error

Error 3007: Problem in Mapping Fragments starting at lines 186, 205: Non-Primary-Key column(s) [WheelID] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.
I found several places on the web describing this error, but I simply don't understand them. (confused smiley goes here)
One
Two
Three
Four
There is something pretty fundamental here, I must be missing. Can you explain it, so that I understand it? Maybe using my real life example below?
Foreign key 1:N Wheels.Id -> Slices.WheelId
I add them to entity framework, and WheelId is not visible in the Slices-entity.
Doing some workaround (deleting the relationship from the db before adding tables to EF - then re-creating it and updating EF) I managed to get the WheelId to stay in Slices, but then I get the error mentioned at the top.
Since Slices.WheelId is an FK, you cannot expose it in your client model, period. There are ways to get the value, though.
var wheelId = someSlice.Wheels.ID;
Update In EF 4 you can do this by using FK Associations instead of independent associations.
Try to remove foreign property column from Entity set using entity model design it will solve your problem
For example
We have two tables one is customer and other one is order, using entity model design we added association between customers and orders when we do this Ado.net entity framework i will add navigation properties to both below tables.
Like
Customer.Orders - Here order is list
Order.Customer
One - Many relation.
So we need to remove property from with name CustomerId[Foreign key column] from Order entity set.
For reference:
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2823634f-9dd1-4547-93b5-17bb8a882ac2/
I was able to overcome this problem by the following steps:
right click the designer window
Select 'update model from database'
Select Add AND make sure that the 'Include foreign key columns in the model' checkbox is selected.
click on Finish...
I had set foreign keys up in the database but framework still wasn't pulling them in correctly. So I tried to add the association myself. 
However, when I did this I would get a mapping error. It took me A WHILE but I figured out. What I did was set up the association using the entity toolbox association tool and then you have to double click on the association (1 to many) line and set the primary and foreign key there. Hopefully, this to help others who might have the same problem. I couldn't find the answer anywhere.
I had this problem for quite a different reason, and the message was slightly different; it didn't say "data inconsistency is possible because the corresponding conceptual side properties can be independently modified."
I have a table involved in my model with a binary column where I store image data. I only want this data returned when I need it (performance is a feature), so I split the table using a method similar to this. Later on, I added a property to that table, then updated the model from the database. The wizard added the property to both entity types that refer to the table with the added property. I had to delete it from one of them to solve the error.
I've had this happen because Entity Framework Update wizard mismapped some keys (or did not update?). As a result, some columns were mistakenly labeled as keys, while actual key columns were treated as plain columns.
The solution was to manually open EDMX file, find the entities, and update the keys.
Couldn't get any of the answer to work with EF6. The problem seems to be the framework doesn't import the foreign keys correctly as Associations. My solution was removing foreign keys from the tables, and then manually adding the associations using Entity Framework model, using the following steps: Entity Framework - Add Navigation Property Manually
For LinQ to Entities queries in EF1, my workaround for not having access to the foreign key as a property is with the following code, which does not produce a join query to the associated table:
dbContext.Table1s.FirstOrDefault(c => (int?)c.Table2.Id == null)
i.e, the generated SQL is:
...WHERE ([Extent1].[Table2Id] IS NULL)...
Solution is to allow deleting Rule = Cascade on Sql association.
Same thing as to be done on .edmx model, adding element to
association:
<Association Name="FK_Wheels_Slices">
<End Role="Wheels" Type= "your tipe here" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
</Association>
I had a table already mapped in EF. I added two more tables which had foreign keys in the previously added table. I then got the 3007 error.
To fix the error I deleted all three tables from the EDMX file, and then re-added them all at once together (via "Update Model from Database..."), instead of in stages.
I checked my Error List window and noticed I had errors in the model. Fixed them and all is well
in my case I solved this error by tick (include foreign key columns in the model)
- update Model from database
- tick (include foreign key columns in the model)
- finish