0

我有我的 3 门课:

public class ApplicationUser : IdentityUser
{
    // all the default ApplicationUser properties etc...
    public virtual ICollection<UserShips> UserShips { get; set; }
    // other properties ...
}

public class Ship
{
    public int ShipId {get; set;}
    public virtual ICollection<UserShips> UserShips { get; set; }
    // other ship properties
}

该船表已经有数据插入到表中,该数据是静态的,不能由仅访问的用户更改

public class UserShips 
{
    public int UserShipId {get; set;}
    public int ShipId {get; set;}
    public string Id {get; set;}
    public virtual ApplicationUser ApplicationUser {get; set;}
    public virtual Ship Ship {get; set;}
}

所以这就是我到目前为止所拥有的,我已经使用 Fluent API 尝试了一些不同的实体映射,但没有成功。

我想要实现的是:每个用户可以有很多用户,每个用户可以有一个 shipid(基于数据库中的条目)和一个用户 ID,任何人都可以帮助我解决我的困境吗?非常乐意发布您需要的更多信息!

4

1 回答 1

0

如果您愿意使用 DataAnnotations,您可以尝试一下(您在帖子中从未提到解决方案必须使用 Fluent Mappings)。

[Table("user")]
public class User
{
[Key,Column("id")]
public string Id {get; set;
[ForeignKey(nameof(Id))]
public virtual ICollection<UserShips> UserShips { get; set; }
// other properties ...
}

[Table("ship")]
public class Ship
{
[Key, Column("shipid")]
public int ShipId {get; set;}
[ForeignKey(nameof(ShipId))]
public virtual ICollection<UserShips> UserShips { get; set; }
// other ship properties
}

[Table("userships")]
public class UserShips 
{
[Key,Column("usershipid")]
public int UserShipId {get; set;}
[Column("shipid")]
public int ShipId {get; set;}
[Column("id")]
public string Id {get; set;}
[ForeignKey(nameof(Id))]
public virtual User User {get; set;}
[ForeignKey(nameof(ShipId))]
public virtual Ship Ship {get; set;}
}
于 2018-07-21T23:17:04.873 回答