English 中文(简体)
how do i create a composite key that comprises a foreign key with code first?
原标题:

I am using EF4 code first and want to generate a composite key which is made of a class property and foreign key. I have two classes: Order and Company. The Order class holds a reference but this will not necessarily be unique between companies. So I intend to use a composite key made up of Reference and Company.CompanyId.

I have tried using the following to set it but I get an error message "Key expression is not valid".

modelBuilder.Entity<Order>().HasKey(o => new { o.Reference, o.Company.CompanyId });

I have also tried

modelBuilder.Entity<Order>().HasKey(o => new { o.Reference, o.Company });

and this fails.

these are my classes:

public class Order
{
   public string Reference { get; set; }
   public Company Company { get; set; }
}

public class Company
{
   public int CompanyId { get; set; }
   public virtual ICollection Orders { get; set; }
}

Any help would be greatly appreciated.

问题回答

As Antony Highsky mentioned, you can only use scalar properties in the key. So, you will need to add a foreign key (scalar property) to the Order class and associate it with the navigation property Company as shown below:

public class Order
{
    public string Reference { get; set; }

    public int CompanyId { get; set; }

    [RelatedTo(ForeignKey = "CompanyId")]
    public Company Company { get; set; }
}

And then create the composite key using the model builder:

modelBuilder.Entity<Order>().HasKey(o => new { o.Reference, o.CompanyId }); 

Note that data annotations (RelatedTo attribute) were introduced with the Entity Framework CTP 3. For another option that only uses data annotations instead of HasKey method, see this post:

One thing that doesn t look quite right is your use of the non-generic version of ICollection. Try this:

public virtual ICollection<Order> Orders { get; set; }

Did you try this?

modelBuilder.Entity().HasKey(o =>o.Reference ); modelBuilder.Entity().HasKey(o =>o.CompanyId );

According to this source, only scalar properties are allowed in the key. Navigation properties are not.





相关问题
named connection not found (Entity framework problem)

I m building multi-project Application where some UserControl, the user control has a Entitymodel object (myDBContainer db = new myDBContainer()), when i drop my user control on my a form i got the ...

EF4 POCO - Updating a navigation property

I have a Recommendation object and a FeedbackLevel object and the FeedbackLevel object is a navigation property inside a Recommendation object Recommendation int EntityKey; FeedbackLevel Level; ...

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven t found a way to declare unique constraints or check constraints. Is this feature missing on the designer or on the ...

Using sql date data type and EF4

I have a table w/ a sql date data type. When I look at the EDM markup, the storage element reflects this. The conceptual entity has a data type of DateTime (there doesn t appear to be a Date data type)...

RIA services, EF and stored procs

I have this problem for 2 months now How can I use stored procedures whith RIA. I was using LinqToSql and everithing works fine. I ve made an class in designer and mapped it to a SP. Now in EF I ...

Mocking Entity Context in EF4

I am using VS2010 B2 and EF4 B2 and trying to use Rhino Mocks to mock the entity context generated by EEF. var context = MockRepository.GenerateMock<SomeDBEntities>(); IObjectSet<TxMode> ...

热门标签