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.
Related
i have a an .xcdatamodeld which already has 2 entities, i have added another entity into this file(am not sure whether this is the right way to add a new entity), anyways my question is, do i need to implement any kind of migrations to take care of users who already have the app installed on their devices.
Please note, i have not modified any columns or schema on already existing entities.
cheers.
You don't need to do migration if you just only adding new entity, assuming that it doesn't have relationship with the other. According to raywenderlich.com, you have to do migration for the following scenario:
Deleting entities, attributes or relationships.
Renaming entities, attributes or relationships using the renamingIdentifier.
Adding a new, optional attribute.
Adding a new, required attribute with a default value.
Changing an optional attribute to non-optional and specifying a default value.
Changing a non-optional attribute to optional.
Changing the entity hierarchy.
Adding a new parent entity and moving attributes up or down the hierarchy.
Changing a relationship from to-one to to-many.
Changing a relationship from non-ordered to-many to ordered to-many (and vice versa).
Quick question that requires a long explanation..
Say I have two tables - one is an item table (say 'Users') and another is a definition table - like 'Custom Properties'. Many different items in the system ('Users', 'Articles', 'Posts', etc) can all have custom property defined to them, and these are stored in the 'Custom Properties' table. So, for example, the 'Custom Properties' table looks like:
CREATE TABLE [CUSTOMPROP_DEFINITION] (
[ITEM_TYPE] INTEGER NOT NULL,
[POSITION] INTEGER NOT NULL,
[NAME] NVARCHAR(MAX) NOT NULL
)
Simple little table. Each item has a 'item_type' id (for example, a user is an item type of 1. Article would be an item type of 2, and so on), so this table could have multiple rows for each item. Essentially, this table's metadata for the other tables.
I want to create a navigation property on my Users table, that will link to all the entries in the props table where 'item_type' == 1.
What's the best way of going about this? From the way I see it, there are two options -
(1) Creating a navigation property through the EDMX and letting it populate it automagically. (This is preferred, but having troubles implementing..) or
(2) Creating a property in the partial class, and having that load everything manually.
The issue with #2 is that it would (could potentially?) be slower than having the entity framework handle loading.
The issue with #1 is that.. no matter what I try, I can't get a NavigationProperty defined that will handle it. Because the primary key is a fixed number - i.e. It will always be '1' for ALL Users, always be '2' for all articles, etc.. - I haven't been able to find a way to hook into that.
Thoughts?
--Mike.
What you're describing isn't really a "Navigation Property" in terms of what EF defines as a naviation property. A navigation property in EF terms follows a [usually] primary key - foreign key reference in the database schema itself. And, AFAIK, the only way to get that navigation property in the EDMX is for there to actually be a FK involved.
You could, obviously (and probably do) have a FK here, but that's not entirely what you want, because that FK is going to return all instances of your custom properties for the given primary key. What you want is instances of only a particular type; and I don't think there is a "off the shelf" way of doing this in EF.
What you probably want to do is implement a Stored Procedure, and bring that into your model; you could then implement this is a property (or probably more appropriately a method) on your entity.
Alternatively, you could just create the FK, have your entity load all of the custom properties, and then write "helper properties" that do simple LINQ based .Where() filters.
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).
I have these two domains Car and Driver which have many-to-many relationship. This association is defined in table tblCarsDrivers which has, not surprisingly, primary keys of both the tables BUT additionally also has another boolean field deleted. Herein lies the problem. When I find/get query on domain Car, I am fetched all related drivers irrespective of their deleted status in tblCarsDrivers, which is expected.
I need to put a clause/constraint to
exclude the deleted associations from the
list of fetched records.
PS: I tried using an association domain CarDriver in joinTable name but that seems not to work. Apparently it expects only table names, not maps.
PPS: I know its unnatural to have any other fields besides the mapping keys in mapping table but this is how I got it and it cant be changed.
Car domain is defined as such -
class Car {
Integer id
String name
static hasMany = [drivers:Driver]
static mapping = {
table 'tblCars'
version false
drivers joinTable:[name: 'tblCarsDrivers',column:'driverid',key:'carid']
}
}
Thanks!
I know its unnatural to have any other
fields besides the mapping keys in
mapping table but this is how I got it
and it cant be changed.
This is not at all unusual. If you want to store properties about the relationship, this is the obvious solution. You should reinstate your association domain CarDriver which has a deleted property in addition to a relationship to Car and Driver, and you should then be able to write a query which excludes the deleted drivers.
A comprehensive example of how to define such a mapping is provided here.
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.