【发布时间】:2016-05-20 12:21:44
【问题描述】:
我与一些附加字段存在多对多关系。但是由于在多对多关系中添加了可能适用于其他关系的照片,因此我想将其分开,因此我可以通过更改一对多关系来更改它。这是模型
public class Segment
{
public int SegmentId { get; set; }
public int ConnectionPointIdEnd { get; set; }
public string ConnectionName { get; set; }
public string ConnectionInformation { get; set; }
public string Image { get; set; }
public string Direction { get; set; }
public ICollection<ConnectionPointRoute> ConnectionPointRoutes { get; set; }
}
public class ConnectionPointRoute
{
public int ConnectionPointId { get; set; }
public int RouteId { get; set; }
public int SegmentId { get; set; }
public int Position { get; set; }
public ConnectionPoint ConnectionPoint { get; set; }
public Route Route { get; set; }
public Segment Segment { get; set; }
}
模型构建器看起来像这样:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ConnectionPointRoute>()
.HasKey(c => new { c.ConnectionPointId, c.RouteId, c.SegmentId });
modelBuilder.Entity<ConnectionPoint>()
.HasMany(c => c.ConnectionPointRoutes)
.WithRequired(x => x.ConnectionPoint)
.HasForeignKey(c => c.ConnectionPointId);
modelBuilder.Entity<Route>()
.HasMany(c => c.ConnectionPointRoutes)
.WithRequired(x => x.Route)
.HasForeignKey(c => c.RouteId);
modelBuilder.Entity<Segment>()
.HasMany(c => c.ConnectionPointRoutes)
.WithRequired(x => x.Segment)
.HasForeignKey(c => c.SegmentId);
}
这一切都适用于获取项目,但由于某种原因,它不允许我发布新路线,例如,它给我错误:
"违反了多重性约束。角色 关系的“Segment_ConnectionPointRoutes_Source” 'InBuildingNavigator.Data.Models.Segment_ConnectionPointRoutes' 有 多重性 1 或 0..1。”
有什么想法吗?
【问题讨论】:
标签: c# asp.net entity-framework post asp.net-identity