views:

132

answers:

1

Hi,

I have a legacy database which has two collumns and I want to map them as 1 ID is this possible

for example

    public class Product
{ 
public string ProductID {get;set;}
public string ShortDescription {get;set;}
public string UserName {get;set;}
}

then my Modelbinder looks like this

modelBinder.Entity<Product>().
HasKey(p=>p.ProductID)
.MapSingle(product =>
new {
 colShotDesc = product.ShortDescription,
 colUser = product.UserName
}
).ToTable("Products");

What I need would be something like ProductID = ShortDescription + UserName in the mapping... because these two collumns share a unique key contraint...

Don't know if this makes sens but any suggestions would be great... Please don't ask about the database design => this is like it is and should not be changed... that's why I thought EF code-first could help me (hope cross fingers)... because it looks like the db hasn't got pk defined just unique key constraints...

anyway ... help would be terrific..

A: 

Actually, you want to map a single conceptual property to a couple of storage columns. There is a code that will concatenate the values from columns into property, so far so good.
But let's imagine the process of adding a new Entity to the context. So, we have set a value for the property. How should EF know the rule to write the value of this property to both columns?
Not sure this scenario is possible to implement.

Devart
Yes I agree it is not so clean but, this is the db I am facing. Since the two columns have a unique constraint on database level, it should be asured that they will be set. I also work with Validation to make sure that they are set. I want my solution in Code a litle cleaner so I am looking for the composite ProductID. How can I assemble my concatenated fields in code-first?
server info