0

I am having a look at Entity Framework and am wondering how to join 2 objects where one object contains a row version column.

For example

public class Product
{
    [Key, Column(Order = 1)] 
    public Guid ProductGuid {get;set;}
    [Key, Column(Order = 2)] 
    public int RowVersion {get;set;}

    public string DisplayName {get;set;}
    // ... more properties ...
}

public class DeviceConfiguration
{
    [Key]
    public Product TheProduct {get;set;}
    [Key]
    public string WorkstationName {get;set;}

    public string ConfigurationString {get;set;}
}

How can I get DeviceConfiguration to pick up the Product row with the highest value of RowVersion?

paul
  • 13,312
  • 23
  • 81
  • 144
  • Could you give us some details about the usage context for which you need this constraint? – Alex Filipovici Jul 22 '13 at 07:07
  • 1
    Highest/lowest does not work with row versions in EF: http://stackoverflow.com/a/17770833/861716. Row versions are not meant to have any business meaning anyway. – Gert Arnold Jul 22 '13 at 07:11
  • @AlexFilipovici - I want to be able to edit details in Product and have a kind of versioning (also Checkin/out). When a user fetches DeviceConfiguration for his workstation, he gets the latest version of Product – paul Jul 22 '13 at 07:29
  • 1
    Why don't you create a view in the database layer which selects the highest `RowVersion` products and then join the `Product` and `DeviceConfiguration` entities by using only the `ProductGuid`? – Alex Filipovici Jul 22 '13 at 07:36
  • @AlexFilipovici - thanks! I'm very new to EF so could you point me to an example (or post an answer) showing how I might do this. – paul Jul 22 '13 at 07:50

1 Answers1

1

From what you described, you already have a database and you would like to develop an application by using that database. Here's an excellent resource about working with the EF which also covers the scenario you are currently facing:

Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

Take a look at the Entity Framework Development Approaches sub-chapter, probably you'll be interested to know more details about the:

Database First

If you already have a database, the Entity Framework can automatically generate a data model that consists of classes and properties that correspond to existing database objects such as tables and columns. The information about your database structure (store schema), your data model (conceptual model), and the mapping between them is stored in XML in an .edmx file. Visual Studio provides the Entity Framework designer, which is a graphical designer that you can use to display and edit the .edmx file. The sections Getting Started With the Entity Framework and Continuing With the Entity Framework in the Web Forms tutorial series use Database First development.

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78