The code for one-to-many relationships in Entity Framework comes first.

entity-framework one-to-many relationship

Question

I'm just getting started with EF 4.1. I think the code first method is the best since I was using NHibenate. I struggle with accurate one-to-many (or many-to-one) relationship mapping. Consider that I have two entities:

class ClientModel
{
    int ClientID;
    string Name;
    virtual IList<OrderModel> Orders;
}

class OrderModel
{
    int OrderID;
    string Details;
    virtual ClienModel Client;
}

When I leave it like way, a database generation error occurs because the keys to the tables are missing. I discovered that I could correct that by either adding [Key] annotation or changing the names of the keys to ID (albeit this is against my naming convention). The names of the tables are incorrect even after I add this annotation; they are the same as class names but with a "s." So I created mappings and attempted to utilize the Fluent API. However, if I configure mappings exactly like this:

class ClientMapping
{
    ClientMapping()
    {
        this.HasKey(e => e.ClientID).Property(e => e.ID).HasColumnName("ClientID");
        this.Property(e => e.Name).HasColumnName("Name");
        this.HasMany(e => e.Orders).WithOptional().Map(p => p.MapKey("OrderID")).WillCascadeOnDelete();
        this.ToTable("Clients");
    }
}

class OrderMapping
{
    OrderMapping()
    {
        this.HasKey(e => e.OrderID).Property(e => e.OrderID).HasColumnName("OrderID");
        this.Property(e => e.Details).HasColumnName("Details");
        this.HasRequired(e => e.Client).WithMany().Map(p=>p.MapKey("Client")).WillCascadeOnDelete(false);
        this.ToTable("Orders");
    }
}

The database's table relationships are doubled. How should one-to-many relationships be implemented when adopting the code-first approach? Is my way of thinking correct, or am I taking the incorrect tack?

EDIT

Okay, I followed @Eranga's instructions exactly, yet the issue persists. Although the database has certain Orders with Orders, when I get Client from the database, its Orders attribute is nil. Client.Client.ClientID == ClientID.

1
3
9/23/2011 7:13:53 AM

Accepted Answer

Both properties involved in the connection must be mapped. You must includeClientID table Orders' column.

class ClientMapping
{
    ClientMapping()
    {
        this.HasKey(e => e.ClientID).Property(e => e.ID).HasColumnName("ClientID");
        this.Property(e => e.Name).HasColumnName("Name");

        this.HasMany(e => e.Orders).WithRequired(o => o.Client)
           .Map(p => p.MapKey("ClientID")).WillCascadeOnDelete();

        this.ToTable("Clients");
    }
}

class OrderMapping
{
    OrderMapping()
    {
        this.HasKey(e => e.OrderID).Property(e => e.OrderID).HasColumnName("OrderID");
        this.Property(e => e.Details).HasColumnName("Details");
        this.ToTable("Orders");
    }
}

It is sufficient to configure the relationship from a single entity.

6
9/21/2011 2:06:27 PM

Popular Answer

When I couldn't figure out how this worked, it helped me, so it could assist you too:

If your courses were structured like this:

class ClientModel
{
    int ClientId;
    string Name;
}

class OrderModel
{
    int OrderId;
    string Details;
    int ClientId;
}

This would therefore represent two tables in your database that "wouldn't" be linked together by a foreign key (instead, they would be linked together by theClientId in theOrderModel ) and you could get information from the database using commands like "GetAllOrdersWithSomeClientId" and "GetTheClientNameForSomeClientId." BUT deleting a file might cause issues.Client by using the database. Since there would still be some thenOrders which would includeClientId It is absent from theClient table, which would cause your database to exhibit abnormalities.

The virtual List<OrderModel> Orders; (in theClientModel ) andvirtual ClienModel Client; (in theOrderModel (also known as the foreign key between the tables) are required to establish the link.ClientModel and OrderModel .

I still don't know how I feel about one issue. that which isint ClientId; in theOrderModel . It must, I presume, have the same name as theClientId in theClientModel in order for the entity framework to understand which two characteristics the foreign key must link. It would be helpful if someone could go into further detail.

Additionally, add the following to your DbContext constructor if anything goes wrong:

this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow