我有两个模型:
public class Exchanger : BaseContract
{
[DataMember, Key, Column(TypeName = "bigint")]
public long Id { get; set; }
....
[DataMember]
public virtual ICollection<PaymentSystem> PaymentSystems { get; set; }
}
[DataContract, Serializable]
public class PaymentSystem : BaseContract
{
[Key, Column(TypeName = "bigint"), DataMember]
public long Id { get; set; }
...
[DataMember, JsonIgnore]
public virtual ICollection<Exchanger> ExchangersSupport { get; set; }
}
和流利的 api 方向具有多对多的关系:
modelBuilder.Entity<Exchanger>()
.HasMany(t => t.PaymentSystems)
.WithMany(t => t.ExchangersSupport)
.Map(m => m.ToTable("ExchangerToPaymentSystem"));
插入代码:
public void Create(Exchanger ex, long clientId)
{
if (_context != null)
{
ex.ClientId = clientId;
ex.LastTimeUpdated = DateTime.UtcNow;
_context.Exchangers.Add(ex);
_context.SaveChanges();
}
}
当我在 Exchanger 表中插入新条目时,EF 在 ExchangerToPaymentSystem 表中创建条目时,它也会同时在 PaymentSystem 表中创建相同的条目。当我更新时,没有任何反应。我做错了什么?