EF4 Code-First CTP5 Many-to-one

Posted by Kevin McKelvin on Stack Overflow See other posts from Stack Overflow or by Kevin McKelvin
Published on 2011-01-01T18:34:05Z Indexed on 2011/01/01 19:53 UTC
Read the original article Hit count: 231

Filed under:
|
|

I've been trying to get EF4 CTP5 to play nice with an existing database, but struggling with some basic mapping issues.

I have two model classes so far:

public class Job
{
    [Key, Column(Order = 0)]
    public int JobNumber { get; set; }
    [Key, Column(Order = 1)]
    public int VersionNumber { get; set; }

    public virtual User OwnedBy { get; set; }
}

and

[Table("Usernames")]
public class User
{
    [Key]
    public string Username { get; set; }

    public string EmailAddress { get; set; }

    public bool IsAdministrator { get; set; }
}

And I have my DbContext class exposing those as IDbSet

I can query my users, but as soon as I added the OwnedBy field to the Job class I began getting this error in all my tests for the Jobs:

Invalid column name 'UserUsername'.

I want this to behave like NHibernate's many-to-one, whereas I think EF4 is treating it as a complex type. How should this be done?

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework-4