0

i have a few entities. Each entity includes many optional documents defined in the inherited BaseEntity.

public class Address : BaseEntity
{

    public virtual Address Parent { get; set; }

    public string Name1 { get; set; }
    public string Name2 { get; set; }
    public string Additional { get; set; }

    public string Street { get; set; }
    public string HousNr { get; set; }
    public string ZipCode { get; set; }
    public string City { get; set; }

    public virtual Document Image { get; set; }

    public virtual ICollection<AddressContact> AddressContacts { get; set; }
    public virtual ICollection<Address> AddressPersons { get; set; }
}

public abstract class BaseEntity
{

    public Guid Id { get; set; }

    public bool IsDeleted { get; set; }
    public bool IsSystem { get; set; }
    public bool IsActive { get; set; }

    public virtual ICollection<Document> Documents { get; set; }

    public DateTime CreatedTime { get; set; }
    public string CreatedUser { get; set; }

    public DateTime? LastModifiedTime { get; set; }
    public string LastModifiedUser { get; set; }

}

and my Document-Entity:

public class Document : BaseEntity
{

    public string Filename { get; set; }

    public string MIMEType { get; set; }
    public int? Length { get; set; }

    public byte[] Content { get; set; }

}

and here my base mapping:

public abstract class  BaseEntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity> where TEntity : BaseEntity
{

    public abstract string TableName { get; }
    public virtual bool HasDocument { get {return false;} }

    public BaseEntityConfiguration()
    {
        HasKey(x => x.Id);

        Property(x => x.CreatedUser).IsRequired().HasMaxLength(100);

        if (HasDocument)
        {

            //TODO: general-Mapping for Documents??

        }
        else
        {
            Ignore(x => x.Documents);
        }

        ToTable(TableName);

    }

}

how can i define my code-first-mapping, so that in the end only one general document table for all comming entities exists?

Ali Ramezani
  • 63
  • 1
  • 4
  • 2
    I'm not really sure what you're asking, can you explain further? You have lots of example, but your question is unclear. – Erik Funkenbusch Apr 10 '12 at 15:38
  • I would create a simple EntityTypeConfiguration class per type to start with, simple and clear.. Further, if you do nothing special Entity Framework will apply TPH mapping, which I think you're after (_one general document table_). It is not necessarily the best mapping strategy! – Gert Arnold Apr 10 '12 at 17:49
  • i want only **one** document table in database. i will have a lot of entities like address with the same relation to document-entity. i would do that without a fk-column for each comming entity... – Ali Ramezani Apr 11 '12 at 07:18
  • OK, that is about polymorphic assiociations. there are [several ways to implement those](http://stackoverflow.com/questions/8895806/how-to-implement-polymorphic-associations-in-an-existing-database). With EF I would choose a model with foreign keys in the database: option 3 [here](http://stackoverflow.com/questions/7000283/what-is-the-best-way-to-implement-polymorphic-association-in-sql-server) – Gert Arnold Apr 12 '12 at 09:58
  • polymorphic assiociations is the key! thank you! – Ali Ramezani Apr 17 '12 at 11:13

0 Answers0