Witam,
Wykorzystuję mechanizm opisany tu:
http://atmist.com/blog/base-class-for-entity-framework-code-first/
Mam bazową klasę dla wszystkich encji, która zawiera Id, DataCreated, DataModified:
public abstract class EntityObject : IEntityObject
{
protected EntityObject()
{
this.State = EntityState.Incomplete;
this.DateCreated = DateTime.Now;
}
[Key]
public long Key { get; internal protected set; }
public EntityState State { get; internal set; }
public DateTime DateCreated { get; protected set; }
public void SetCreationInfo(long userId, DateTime createdAt)
{
DateCreated = createdAt;
}
}
Jak zaimplementować relację 1:1 ? Gdy robię to w ten sposób:
public class User : EntityObject
{
public virtual UserDetails UserDetails { get; set; }
}
public class UserDetails : EntityObject
{
[ForeignKey("User")]
public long UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public User User { get; set; }
}
Wydaję mi się, że w klasie UserDetails ten atrybut ForeignKey("User") powinien być powieszony nad właściwością Key z klasy bazowej, ale jak to zrobić?
To wyskakuje taki błąd:
UserDetails_User_Source: : Multiplicity is not valid in Role 'UserDetails_User_Source' in relationship 'UserDetails_User'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.