Free data mappings / annotations for unique columns and navigation properties with different names in Entity Framework 4.1?

First, is there a way to tell EF 4.1 that the column must be unique using data annotations or a free API?

Secondly, regarding navigation properties, I have 2 Document and User classes. In the document class, I have a property for OwnerId (UserId) and a property for Owner (User). How to tell EF 4.1 that OwnerId is really UserId and Owner is a navigation property for the user?

Document Class:

public abstract class Document: BaseEntity
    {
        public bool IsActive { get; set; }
        public string Description { get; set; }
        //The UserId
        public Guid OwnerId { get; set; }
        //The User
        public User Owner { get; set; }

    }
+3
source share
1 answer

Entity framework does not support unique keys, so the answer to your first question is no.

OwnerId Owner, , -. , :

public abstract class Document: BaseEntity
{
    public bool IsActive { get; set; }
    public string Description { get; set; }
    [Column("UserId"), ForeignKey("Owner")]
    public Guid OwnerId { get; set; }
    public User Owner { get; set; }
}

, , , FK .

:

modelBuilder.Entity<Document>()
            .Property(d => d.OwnerId)
            .HasColumnName("UserId");
modelBuilder.Entity<Document>()
            .HasRequired(d => d.Owner)
            .WithMany(...)
            .HasForeignKey(d => d.OwnerId);
+5

All Articles