Defining ForeignKeys with Code First - c#

I'm trying to define a relationship between 2 tables with the ForeignKeyAttribute.
I came a cross a few sites that described an interesting method of doing this with the ForeignKeyAttribute.
Here are the two code samples:
The first one:
public class Customer
{
public int ID { get; set; }
public int OrderID { get; set; }
// Some other properties
[ForeignKey("OrderID")]
public virtual Order Order { get; set; }
}
public class Order
{
public int ID { get; set; }
public int CustomerID { get; set; }
// Some other properties
[ForeignKey("CustomerID")]
public virtual Customer Customer { get; set; }
}
The second one:
public class Customer
{
public int ID { get; set; }
[ForeignKey("Order")]
public int OrderID { get; set; }
// Some other properties
public virtual Order Order { get; set; }
}
public class Order
{
public int ID { get; set; }
[ForeignKey("Customer")]
public int CustomerID { get; set; }
// Some other properties
public virtual Customer Customer { get; set; }
}
In the first code sample, the ForeignKeyAttribute is placed on the public virtual Customer Customer { get; set; }.
And in the second code sample on the public int CustomerID { get; set; } (Order- and CustomerID).
My question is, how do I know which method to use in which situation?
I know this could be done too using Fluent API, but that's irrelevant at the moment for this question.

First, just want to say that you do not need to place the ForeignKey attribute on any property. Simply doing the following should be enough:
public class Customer
{
public int ID { get; set; }
//EF will create the relationship since the property is named class+id
//the following is not necessary, is just good practice
//if this is omitted EF will create a Order_Id on its own
public int OrderID { get; set; }
// Some other properties
public virtual Order Order { get; set; }
}
public class Order
{
public int ID { get; set; }
// no need to include the id property
// Some other properties
public virtual Customer Customer { get; set; }
}
Having said that, the answer is that the ForeignKey construct takes in a string parameter. If you place it on the foreign key property it should have the name of the navigation property, if you place it on the navigation property it should have the name of the foreign key. Where you place it is completely up to you, as long as you keep in mind what string value to use.

Related

Confusion on adding list of orders into database using Entity Framework Core

i am making a restaurant ordering system, all is going good except im getting confused at this part. Adding orders made by the customer to the database.
i have tables by the name Order and OrderDetails which have the following properties
public class Order
{
public Guid ID { get; set; }
public **Customer** CustomerID { get; set; }
public int Quantity { get; set; }
public DateTime OrderedDateOn { get; set; }
public string OrderDateFor { get; set; }
public decimal TotalAmount { get; set; }
public string Status { get; set; }
public string? DetailRequest { get; set; }
public string? DetailReply { get; set; }
public int ItemCount { get; set; }
}
and here is the order table
public class OrderDetails
{
public Guid ID { get; set; }
public Order OrderID { get; set; }
public List<Item> Item { get; set; }
public int AmountOfItem { get; set; }
}
so how do i go about register the order made from the user.
do i first send the order to be registered, add the items along side the order ID to the OrderDetails table?
i am genuinely confused as to how the order will be registered.
Have a look at Changing Foreign Keys and Navigations.
In your case it looks like CustomerID and OrderID should be guids and
and later you should have a navigation property:
public Customer Customer { get; set; }
Here's an example from the linked article:
public class BlogAssets
{
public int Id { get; set; } // Primary key
public byte[] Banner { get; set; }
public int? BlogId { get; set; } // Foreign key
public Blog Blog { get; set; } // Reference navigation
}

EF6: Using collection property without creating FK field in child table

I'm studying EF6 and think I know quite a bit already, but couldn't find a good solution (yet) for this:
Suppose I have the following model classes:
class LivingRoom {
public int Id { get; set; }
public string Name { get; set; }
public PersonTypeId { get; set; }
public IList<Person> Persons { get; set; }
}
class Person {
public int Id { get; set; }
public int PersonTypeId { get; set; }
public string Name { get; set; }
}
With these model classes I'm able to save and load via DbContext without any problem. Thanks to the navigation property in the "parent" LivingRoom class, the Persons collection will be included in this process. I don't have to load/save them separately.
UPDATE: Forgot the logical PersonTypeId field which will be used for determining which Persons should be in the collection property.
So far so good.
But EF6 is creating a FK in the Persons table, pointing to the LivingRooms table, which seems logical.
But what if I'm going to use the Persons table for a lot more other parent entities, like eg. "Bus" and "Plane", and therefore don't want to have a dependency (= FK field in LivingRooms table) in the Persons table?
Can I achieve this (don't create the FK field) without breaking the "include child list" load/save process as described?
If yes, how? And if no, why not?
NB: Please understand that I want to learn the best techniques. So good advice, to not doing this, is also welcome.
First , it's better to handle FK in Person Table ourself to do that web have this :
class LivingRoom
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Person> Persons { get; set; }
}
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public LivingRoom LivingRoom { get; set; }
public int LivingRoomId { get; set; }
}
now If you have others Entities Like Bus and ... so we have
public class Bus
{
public int Id { get; set; }
public ICollection<Person> People { get; set; }
}
and Updated Person class is :
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public LivingRoom LivingRoom { get; set; }
public int LivingRoomId { get; set; }
public Bus Bus{ get; set; }
public int BusId { get; set; }
}
you can set FK in Person Table as Nullable to do this :
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public LivingRoom LivingRoom { get; set; }
public int? LivingRoomId { get; set; }
public Bus Bus{ get; set; }
public int? BusId { get; set; }
}
As you can see We set BusId and LivingRoomId as nullable or you can just set one of them that you want
Note : You need to add some mapper to tell EF which field id FK and something like this ,...

Foreign keys with Code First in a Web API

Very simple question but it looks like I'm trying to implement a simple one-to-many relationship between two models.
So far, what I have is this :
A product class :
public class Products
{
public long Id { get; set; }
public long Code { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public Boolean Reviewed { get; set; }
[ForeignKey("BundleId")]
public int BundleId { get; set; }
public virtual Bundles Bundle { get; set; }
}
And the Defects class looks like this:
public class Defects
{
public long Id { get; set; }
public String Description { get; set; }
public String Picture { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("ProductId")]
public int ProductId { get; set; }
public Products Product { get; set; }
[ForeignKey("UserId")]
public int UserId { get; set; }
public virtual Users User { get; set; }
}
I thought that I did not need to add an ICollection of Defects to the Products class because it's a "simple" one-to-many relationship and this code would be enought to be able to get the ID of a Product in the Defects class (I don't need more).
But, of course I get an exception :
The property 'ProductId' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter
How may I solve that issue ?
I might be doing someting wrong with my two foreign keys but since I declared the name of the foreign keys, I assumed it would have been enought.
Thanks for your attention.
This is what your relationship can be distilled to.
Please note that ForeignKey annotation is applied to navigation property with the name of the key property.
If you build one-to-many relationship - then ICollection is absolutely necessary. Otherwise where's the "many"
public class Products
{
public int Id { get; set; }
public virtual List<Defects> Bundle { get; set; }
}
public class Defects
{
public long Id { get; set; }
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public Products Product { get; set; }
}
FK can also be applied to the key property. But in that case you have to put the name of the instance of related class there
public class Defects
{
public long Id { get; set; }
[ForeignKey("Product")]
public int ProductId { get; set; }
public Products Product { get; set; }
}

Enforce child entity uniqueness?

What is the best way to enforce a child entity to be unique? For instance, lets say I have a Customer entity and a child entity collection called MarketingCampaign
public class Customer
{
public int ID { get; set; }
public virtual ICollection<MarketingCampaign> MarketingCampaigns { get; set; }
}
public class MarketingCampaign
{
public int ID { get; set; }
public string Name { get; set; }
}
Lets say that if a customer has the same MarketingCampaign added twice then it would be very bad as they would receive duplicate material.
In my code I could check if it exists before adding it but that relies on everyone knowing it must be unique.
Is there a way to force this on the model (preferably with data annotations)?
You are looking for a one-to-zero-or-one relationship.
You can indeed use DataAnnotations to accomplish what you're trying to do, but you should have an intermediary table that tracks the customer / campaign relationship and has a FK back to a Campaign table. Then with the magic of Entity Framework, it CustomerMarketingCampaignId will be both the PK of CustomerMarketingCampaign and FK back to Customer
public class Customer
{
public int CustomerId { get; set; }
public virtual ICollection<CustomerMarketingCampaign> CustomerMarketingCampaign { get; set; }
}
public class CustomerMarketingCampaign
{
[ForeignKey("Customer")]
public int CustomerMarketingCampaignId
[ForeignKey("Campaign")]
public int CampaignId { get; set; }
}
public class Campaign
{
public int CampaignId {get;set;}
public string Name {get;set;}
}
My final solution for posterity:
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<CustomerMarketingCampaign> CustomerMarketingCampaigns { get; set; }
}
public class MarketingAction
{
public int ID { get; set; }
public string Name { get; set; }
}
public class CustomerMarketingCampaign
{
public int ID { get; set; }
[Index("IX_CustomerAndMarketing", 1, IsUnique = true)]
public int CustomerID { get; set; }
[Index("IX_CustomerAndMarketing", 2, IsUnique = true)]
public int MarketingActionID { get; set; }
// I also have several properties not included for tracking the progress of the campaign
[ForeignKey("CustomerID")]
public virtual Customer Customer { get; set; }
[ForeignKey("MarketingActionID")]
public virtual MarketingAction MarketingAction { get; set; }
}

Entity Framework Code First associations/FK issues and assumptions/defaults

I am very confused by the way Entity Framework is able to pick up on relationships between entities. I have a few questions on this.
In a simple test application, I have a table of people, and a table of notes, and a table of picture assets.
There are many pictures, each is owned by a person (a person can own more than one).
There are many notes, each is owned by a person (a person can own more than one).
and finally a person has a logo which is a picture.
.
public class Person
{
public int ID { get; set; }
public string name { get; set; }
public Picture logo { get; set; }
}
public class Note
{
public int ID { get; set; }
public string Text { get; set; }
public Person Owner { get; set; }
}
public class Picture
{
public int ID { get; set; }
public string Path { get; set; }
public Person Owner { get; set; }
}
When I try to run, I get the error "Unable to determine the principal end of an association between the types ....".
(If I drop the Person.logo field, compile/run, then manually add it in to SQL, along with the FK relationship, it works 100% as expected... I just can't seem to work out how to set this from EF itself).
Can you help with the error? I have read quite a few answers here, but, I just can't seem to adapt it to fix my error.
However, now I have a one to many and a many to one (I don't think this is classed as many to many?), I just don't understand how to create people objects, where a FK is non nullable and the FK doesn't exist yet.
A solution I found, which I really don't like is to make the person.picture column nullable, then create a person, followed by creating a picture, then assign a picture to the person object... but, ideally I don't want it nullable. There should always be a picture.
Old answer assuming your 1:1 relation:
Scroll down for an answer on your clarification.
Here are two ways to achieve this, one way is to remove the cross reference navigation by applying 1:N.
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public virtual Picture Logo { get; set; }
}
public class Note
{
public int ID { get; set; }
public string Text { get; set; }
}
public class Picture
{
public int ID { get; set; }
public string Path { get; set; }
}
This is a really cheap solution, you probably don't want more notes or pictures than persons...
So, use Data Annotations to instruct what the foreign key is, this will keep navigation and 1:1.
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public virtual Picture Logo { get; set; }
}
public class Note
{
[Key, ForeignKey("Person")]
public int OwnerID { get; set; }
public string Text { get; set; }
public virtual Person Owner { get; set; }
}
public class Picture
{
[Key, ForeignKey("Person")]
public virtual int OwnerId { get; set; }
public string Path { get; set; }
public virtual Person Owner { get; set; }
}
As you can see, because of the 1:1 relationship the picture and note will use the same ID. If your key is not named ID, you need to add a KeyAttribute, in this case we also add the ForeignKeyAttribute.
Please note that you should use virtual so that things load only when you request them, you most likely don't want the database to query the Picture information if you only want the name of the Person.
Association properties that are marked as virtual will by default be lazy-loaded. What this means is that if you retrieve a Product entity, its Category information will not be retrieved from the database until you access its Category property (or unless you explicitly indicate that the Category data should be retrieved when you write your LINQ query to retrieve the Product object).
— Scott Gu - Using EF Code First with an existing database
New answer regarding your clarification:
Bulding further on the Foreign Keys by going back to a 1:N structure with ICollection<T>; once you obtain your person like var boss = Person.Find(BossID) you can then access boss.Pictures which will have the various pictures. You can also assign boss.Logo to be one of those pictures.
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public virtual Picture Logo { get; set; }
public ICollection<Picture> Pictures { get; set; }
public ICollection<Note> Notes { get; set; }
}
public class Note
{
public int ID { get; set; }
[ForeignKey("Person")]
public int OwnerID { get; set; }
public string Text { get; set; }
public virtual Person Owner { get; set; }
}
public class Picture
{
public int ID { get; set; }
[ForeignKey("Person")]
public virtual int OwnerId { get; set; }
public string Path { get; set; }
public virtual Person Owner { get; set; }
}
You might be interested in hinting your data members using DataAnnotations as well as taking a look to the current Conventions for Code First.
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public virtual Picture Logo { get; set; }
public ICollection<Picture> Pictures { get; set; }
public ICollection<Note> Notes { get; set; }
}
public class Note
{
public int ID { get; set; }
public int OwnerID { get; set; }
public string Text { get; set; }
[ForeignKey("OwnerId")]
public virtual Person Owner { get; set; }
}
public class Picture
{
public int ID { get; set; }
public virtual int OwnerId { get; set; }
public string Path { get; set; }
[ForeignKey("OwnerId")]
public virtual Person Owner { get; set; }
}
The above code is right, after trying the wrong code several times, I got the right code.
Hope it is usefull for you.

Categories