实体框架代码第一集合协会

时间:2012-04-23 07:54:26

标签: c# asp.net code-first

我遇到EF CodeFirst问题。 我有一些从我的BaseModel继承的模型,我想在一些模型中包含Images。 例如,我有EFModel用户

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; }
}

另一个名为Spot的EF模型

/// <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; }

}

在两个EF模型中,我想要一张照片列表。目前,Photo Entity只有名称和描述(我将在后面使用byte [])

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; }

}

所以一切正常,但我的问题是生成的表。因为Entity使用以下列生成表“dbo.Photos” | ID(PK)|名称|说明| User_ID(FK)| Spot_ID(FK)

但我希望有一个简单的表,我可以轻松地为所有BaseModel使用,而无需在BaseModel类中使用它。但我希望有例如以下列

| ID(PK)|名称|说明| RelationModel_ID(FK)

我真的不知道我怎么做到这一点。我希望有人可以帮助我。

非常感谢

///编辑:

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;
    }
}

0 个答案:

没有答案
相关问题