1

i have a problem with EF CodeFirst. I have some Model inherited from my BaseModel, and i want to include Images in some Models. For example i have EFModel User

public class User : BaseModel<User>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="User"/> class.
    /// </summary>
    public User()
    {
       Photos = new HashSet<Photo>();
    }

    /// <summary>
    /// Images
    /// </summary>
    public ICollection<Photo> Photos { get; set; }
}

And another EF Model named Spot

/// <summary>
/// Eventlocation
/// </summary>
[DebuggerDisplay("Spot: {Name} Coordinate: {Coordinate}")]
public class Spot : BaseModel<Spot>, ISpot
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Spot"/> class.
    /// </summary>
    public Spot()
    {
        Photos = new HashSet<Photo>();
    }

    /// <summary>
    /// Name
    /// </summary>
    [Display(Name = "Spotname", GroupName = "Names", ResourceType = typeof(Texts))]
    [Required]
    public string Name { get; set; }

    /// <summary>
    /// Description
    /// </summary>
    [Display(Name = "Description", Description = "SpotDescription", GroupName = "Names", ResourceType = typeof(Texts))]
    public string Description { get; set; }

    /// <summary>
    /// Geo Position
    /// </summary>
    public GeoCoordinate Coordinate { get; set; }

    /// <summary>
    /// Images
    /// </summary>
    public ICollection<Photo> Photos { get; set; }

}

In both EF Models i want to have a list of photos. Currently the Photo Entity only have Name and description (byte[] i will made later)

public class Photo 
{
    /// <summary>
    /// Identifier
    /// </summary>
    [Key]
    public int Id { get; set; }

    /// <summary>
    /// Imagename
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Imagedescription
    /// </summary>
    public string Description { get; set; }

}

So everything is working, but my problem is the generated table. Because Entity generates the table "dbo.Photos" with following columns | ID (PK) | Name | Description | User_ID (FK) | Spot_ID (FK)

But i want to have simple table that i can easy use for all BaseModels without to need to have it in BaseModel class. But i want to have for example following columns

| ID (PK) | Name | Description | RelationModel_ID (FK)

I really dont know how i can make this. And i hope somebody can help me.

Thank you very much

///EDIT:

BaseModel

public abstract class BaseModel : IBaseModel, IEquatable<BaseModel>
{
    [Key]
[HiddenInput(DisplayValue = false)]
    public Guid Id { get; set; }
}

public abstract class BaseModel<T>:BaseModel
    where T:BaseModel<T>,new()
{
    public static T Create()
    {
        var result = new T(){Id = Guid.NewGuid()};
        result.UpdateOwner();
        return result;
    }
}
4

0 回答 0