I need to add a navigation property between two Entities TableA and TableB
TableA
ID : Primary Key
Code: String (Allows Null)
TableB
BID: Primary Key
Code: String (Allows Null)
Now I want to add a navigation property to these Entities which are related by the code which is not a foreign key. Can anyone tell me how this is possible
It is not possible because code is not PK in any of your tables. Navigation properties follows same rules as database relations - in principal table you must use PK and in dependent you specify FK. Databases also offers selecting unique key in principal table but EF doesn't support unique keys yet.
Related
The picture show relation with two tables in my database;
Well as you can see there's a field called "DeviceTypeID" in the right side table.
Normally EF adds CompanyTypeID column to the ServiceLaburDefinitions model but it's disappeared last time I updated model from the database.
I am searching for a solution for a couple of hours but not able to find any solution. Could anyne suggest a solution?
Thanks.
ServiceLaburDefinitions is the depend entity and has the DeviceTypeID foreign key property defined.
So Entity Framework creates a navigation Key under the hood between the 2 tables based on DeviceTypeId key.
You can see for example how a navigation key is created also in the following example between the foreign key and the primary key
More information about navigation properties can be found here
When you create the model from the database, there is a checkbox marked "Include Foreign Key columns In The Model" - Make sure this is checked.
If I have an entity in EF and I also have foreign key property in that entity. Now I want to update related record. If I use both Foreign Key Association and Independent Association, Then which will be preferred by Entity Framework and why ?
Case : Suppose If I assign different entity to navigation property and different enityId in foreign key property. Then what will be saved in database ?
I think the foreign key in your case. Independent key just for the table's itself
I have the following model with 3 tables and I'm attempting to create an association to the third table CustomerPart which uses a compound key from both of the other tables.
CustomerPart should be accessed via a key of CustomerNo from ShopOrder and Site/CatalogNo from SalesPart.
The only thing I've been able to come up with so far is to create an association from SalesPart to CustomerPart and in code manually select records where CustomerNo on CustomerPart matches the CustomerNo on ShopOrder.
Is there a way to create an association and navigation property for this or am I out of luck?
I am working on a C# project querying a Sql Db by the auto-generated classes by the sqlmetal tool. That works fine.
My problem is that in my DB I have to tables (TableA and TableB) which have a many to many relationship, therefore, there is a third table called TableATableB which holds only the primary keys of TableA and TableB.
So, as expected, my EntityModel has a class named TableA that has a property named TableATableB which is a collection of all related TableATableB rows.
That is the auto-generated property:
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="FK_TableATableB_TableA", Storage="_TableATableB", ThisKey="Id", OtherKey="TableAId", DeleteRule="CASCADE")]
public EntitySet<TableATableB> TableATableB
{
get
{
return this._TableATableB;
}
set
{
this._TableATableB.Assign(value);
}
}
The thing is I would like TableA to have a property named TableB which gives me directly an System.Data.EntitySet<TableB> collection of all related TableB rows, is there a way to do that? I have been trying to do it but I haven't found the way to map it.
Just to clarify, I know a workaround simply could be as follows, but I'm trying to see if there is a better way. Besides, this way how could I know when a TableB object is added to the collection in order to add the respective TableATableB object?
partial class TableA
{
public List<TableB> TableB
{
get
{
List<TableB> tableBRows = new List<TableB>();
foreach (TableATableB tAtB in this.TableATableB)
{
tableBRows.Add(tAtB.TableB);
}
return tableBRows;
}
set
{
// What to do here?
// I need to know when an element is added in order to add its respective
// TableATableB object
}
}
Thanks in advance.
Linq to SQL does not support the behavior you desire. L2S does not support implicit many-many collections like EF.
Entity Framework does, but requires that the all fields of the many-many table (TableATableB in your case) make up the primary key of that table. For example, if TableA's primary key is Id and TableB's primary key is Id then TableATableB must have exactly two fields, TableA_Id and TableB_Id, that are the primary key of that table. If TableATableB were to have three fields in this case like Id, TableA_Id and TableB_Id, and Id is the primary key of the many-many table then EF cannot and will not treat that as an implicit many-many collection.
If I have a table with two foreign key fields to another table, I.E.
Table: User
Field: FK_PrimaryItem_ID
Field: FK_SecondaryItem_ID
Table: Item
Field: ItemID
When I'm using the entity framework, the generated objects become:
User.Item
and
User.Item1
and I can't differentiate between the two of them. I can map back to the name of the foreign key, but this is a difficult way to go about it. How can I find out which one, Item1 or Item is which field?
I would like to leave my EDMX file auto generating if possible.
I've not found any problems with updating my model once I'd changed the name of the Navigation Properties on the design surface.
In general, User.Item would be represent the first column the model came to with that foreign key, and User.Item1 would represent the second column.
But as I said, I just went into the model, and changed the name of the Navigation Properties to more usable names based on the association listed in the Mapping Details.
I had the same problem with a self-referencing key:
PageID
Parent_PageID (refers to PageID)
Until I renamed the Navigation Properties to "Parent" and "Children" respectively. The toughest part was figuring out which is which, which I did by noting the Multiplicity property on the NavigationProperty objects (0..1 for parent, * for children)