Problems with EDM associations - ado.net-entity-data-model

I'm making an OData service and using an EDM to map the structure of my database and specify associations between tables and create Navigation Properties.
I've imported my tables into the EDM using the wizard. Things compile fine until I put in associations. I'm sure I've been able to do this before but for some reason it is screwing up.
I have a table Attribute
AttributeId int PK
AttributeName varchar(100)
and
AttributeItem
AttributeItemId int PK
AttributeID int
AttrributeItemValue varchar(100)
I want to link AttributeId between the two tables with a 0..1 to * relationship (from Attribute to AttributeItem).
I'm constantly getting problems. initially this error:
Error 3027: No mapping specified for the following EntitySet/AssociationSet - AttibuteAttributeItem
Then once I've fiddled with the mapping details of the association I get this error:
Error 3024: Problem in mapping fragments starting at line 91: Must specify mapping for all key properties (AttributeItemId, AttributeId) of End AttributeItem in Relationship AttributeAttributeItem.
I've no idea why it's doing this.
Any idea what I'm missing?
thanks,
B

double click on Assotiation link and set up Referential Constraint. It shouldn't be empty. Use "Generate Database" after that to update EDM model internals.
Also consider to right click on edmx file in solution exprorer and select "Open With..." and open it with XML editor to see at the line of error (91).

Related

Updating entity framework model after changing the data type of column in database table

I am working with entity framework 6 with database 1st approach. I changed the datatype of my columns in the table and after updating my model I got the following error:
Error 1 Error 2019: Member Mapping specified is not valid. The type
'Edm.Int32[Nullable=False,DefaultValue=]' of member 'Warranty' in type
'pjModel.Bill' is not compatible with
'SqlServer.nvarchar[Nullable=False,DefaultValue=,MaxLength=50,Unicode=True,FixedLength=False]'
of member 'Warranty' in type 'pjModel.Store.Bills'.
what I understand that there is some compatibility issue between datatypes of framework and sql server.
Also when I opened the .edmx file with XML viewer, it clearly shows that changes are propagated in the model i.e data type has been updated, but in the diagram view when I right click the particular column it still shows the old datatype.
I can provide more information if required.
The issue has been resolved. I manually change the type from the diagram view and then it get mapped with the model class.
Same problem here, using EF6, after changing on a database table a field from int to bit.
It was not enought to delete property from model designer, and create it again, as the table mapping refered to the old type.
I guess this could be useful for similar cases. The only way to solve it (unless you don't mind deleting entity and updating model from database) is to edit your Entity Data Model as text (by default, DataModel.edmx), locate the entity as <Entity> node inside <Schema> node and find the desired property that indicates the old type. As an example:
<Property Name="MyProperty" Type="int" Nullable="false" />
Simply change Type attribute to the desired type, return to Visual Studio, save the edmx file, and rebuild.

Modify entity framework mappings

I had a column in a table which type was nvarchar(50) and had entity framework to generate te mappings and the model from that table for an asp mvc project, but after tricky error i found that the 50-length was to short for the type of field. I already modified the column to nvarchar(100) and on the mappings folder the Table.cs file has a line this.Property(t => t.AColumn).HasMaxLength(50), i can guess that modifing the value to 100 should do the work, so is this all what i need or do i need Entity Framework to re-do everything, another question is how to make Razor, Entity or any tool to show some kind of message related to the mappings constrains.
Edit:
The error was that AColumn was too short for a form field but it didn't show a error message, it just shows errors from the model attributes or type constrains but not things defined on the mappings like HasMaxLength(50), i want to be able to show the length error on the view if possible. The app just failed with an EntityFramework exception with a rather complicated traceback.
Thanks.

Entity Framework and LLBLGen

I have a database, which has two tables.
The Order table, and the Item table. The relationship is m:1.
I am using LLBLGen as the mapping tool to generate the source file.
I can see that the ItemID from the Item table is displayed in the Order entity from the designer of LLBLGen, which is correct.
However, when I try to call this generated source codes, the ItemID is not listed as a property of my Order class. Actually all my foreign key is gone.
Could anyone teach me how to generate the foreign key as a property?
Thanks a lot
It's a setting. Go to:
Project properties -> Output Setting values Tab -> EmitForeignKeyFields, check the checkbox. Ok -> regenerate code.
It's false by default.
Ps: if you post your question on our own support forums, you might get answers more quickly ;)

Simple one to many relation (association) fails in EF designer

I tried setting up a simple one-to-many relation in Entity Frameworks designer.
The tables are Category (1) and Transaction (N). Here's what I did:
Add "association"
End1 = Category, multiplicity 1, navigation property=Transaction
End2 = Transaction, multiplicity Many, navigation property = Category
Building it gave me the error "No mapping specified". Ok, makes sense. So I added this mapping:
Category
Category.CategoryID = Transaction.CategoryID
But the mapping designer also automatically adds a mapping for the Transaction table, which I cannot figure out how to delete or how to setup:
Transaction
Transaction.TransactionID = ???
Leaving it empty seems most valid, but that gives me: Error 3024 "Must specify mapping for all key properties (TransactionID)"
And trying to set it to a fake int property just hoping it's a compiler bug. But that gives me errors 3002 and 3003.
I dont get what to do. Isnt Associations meant to be used this way?
I suggest creating (or importing from the database) an entity for Catagory and an entity for Transaction. Add scalar properties to each as needed. Next, right-click on your entity, click Table Mapping, and map your entity properties to the table fields. For example, for the Category entity, map CategoryID field to a CategoryID property. Do the same for the other entity. THEN create the association.
Note that associations linked by exposed foreign keys do not have any mappings.
BTW, you'll probably want to add navigation properties as well.

How to get the foreign key in Entity Framework?

I am developing StudentApp in .NET 3.5 SP1 MVC Application.
I have two tables
Course
CourseID, course_Name
Students
studentID, student_Name, courseID(fk)
Now I made StudentApp.dbml which is having both table as entities.
As Foreign key will not be present in student entity,
I can not display courseID in student model, more over i can not generate add, edit, list views.
So tell me how to display courseID(fk) in student & i also want course name instead.
And also dropdownbox showing course name & storing courseID in edit view .
I'm pretty sure you have to load the foreign reference for each entity. Since I have no idea how you've constructed your API, I'll have to give you a pseudocode'ish example, but I think this is what you need to do.
List<Students> studList = [your_db_facade].SelectStudents() // Or however you retrieve your students
foreach (Students singleStudent in studList)
singleStudent.Context.CourseReference.Load() //CourseReference.Load() should be in the framework
Then you get the CourseID and name from the single student entity like
singleStudent.Course.CourseID
singleStudent.Course.course_Name
It could look slightly different for you, but I think the key to solving your problem is CourseReference.Load().
If your using LINQ-to-SQL and created a DBML file in Visual Studio then the foreign keys can be listed through the Course property in the Student object (automatically generated so since it is a one-to-many relationship from Student). Sort of like this:
var studentCourseIds =
from s in context.Students
select s.Course.CourseID;
Since your goal is to find the coursename then it is already accessible with Student.Course.course_Name.
Can you post your DBML? Also, DBML is used in LINQ to SQL (L2S) - EDMX is the mapping used in the ADO Entity Framework. Are you using LINQ to SQL or the Entity Framework (EF)?
No matter which one you are using - they both support Foreign Keys and you would get a property representing either side of the relationship - you don't need to do anything special (the Foreign Key must exist in the database, of course).
In EF, the foreign keys are called "navigtion properties" and they work a little differently to Foreign Keys in L2S. Nothing major, but updating them and "eager loading" are somewhat different.
Just drop the tables onto the map in the designer in Visual Studio (or generate using command line equivalents if you prefer).
Regarding Foreign Keys and Drop Down Lists (and other UI goodness) - I wrote a couple of blog entries on some approaches which might suit you. One part is located here and part two is located here.
if you create the correct relationship in your SQL server database, then when you add the tables to your DBML designer, the relationships will be copied across also and your code will link up automatically.

Resources