gpt4 book ai didi

c# - 如何正确定义 EF Core 创建的这些类之间的关系?

转载 作者:行者123 更新时间:2023-12-03 13:42:35 27 4
gpt4 key购买 nike

首先道歉,这是一个菜鸟问题。

我正在尝试编写一个连接到现有数据库并根据用户输入更改某些值的 c# 程序。 (我知道这听起来可能是个坏主意,但我们有自己的理由……)我决定使用 ef core 连接到数据库。

我用过 Scaffold-DbContext将数据库结构放入我的程序中。
构建成功,但是每当我尝试运行我的程序时,我都会收到以下错误消息:

System.InvalidOperationException: 'The child/dependent side could not be determined for the one-to-one relationship between 'Dad.DadCmu' and 'DadCmu.IddadNavigation'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.'



所以我猜这种关系没有明确定义。然后让我们看一下类:
    public partial class DadCmu
{
public int Iddad { get; set; }
public byte UnitNo { get; set; }

public virtual Dad IddadNavigation { get; set; }
}

所以 IddadNavigation 是 Dad 类型:
public partial class Dad
{
public Dad()
{
DadCh = new HashSet<DadCh>();
DadConnection = new HashSet<DadConnection>();
}

public int Iddad { get; set; }
public string Name { get; set; }
public int? DadType { get; set; }
public short? Active { get; set; }
public DateTime? LastUpdate { get; set; }
public byte? SynchronizationStatus { get; set; }

public virtual DadCmu DadCmu { get; set; }
public virtual DadMasCon DadMasCon { get; set; }
public virtual DadOpc DadOpc { get; set; }
public virtual ICollection<DadCh> DadCh { get; set; }
public virtual ICollection<DadConnection> DadConnection { get; set; }
}

他们的关系应该在 OnModelCreating 内定义:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<DadCmu>(entity =>
{
entity.HasKey(e => e.Iddad);

entity.ToTable("DadCMU");

entity.Property(e => e.Iddad)
.HasColumnName("IDDad")
.ValueGeneratedNever();

entity.HasOne(d => d.IddadNavigation)
.WithOne(p => p.DadCmu)
.HasForeignKey<DadCmu>(d => d.Iddad)
.HasConstraintName("FK_DadCMU_Dad");
});

modelBuilder.Entity<Dad>(entity =>
{
entity.HasKey(e => e.Iddad);

entity.Property(e => e.Iddad).HasColumnName("IDDad");

entity.Property(e => e.LastUpdate).HasColumnType("datetime");
});

}

所以在这里 modelBuilder.Entity<DadCmu>它确实明确提到了一个外键,所以我不太确定问题是什么?
我是否必须在 modelBuilder.Entity<Dad> 内做同样的事情? ?

这段代码会是什么样子?

还是我完全误解了这个问题?

这是可以自动完成的,例如带有 scaffold-dbcontext 的参数? (我的数据库有 180 个表,其中许多表都出现相同的错误。)

在不改变数据库本身的结构的情况下,这一切都可能吗?

非常感谢您提前!同样,这是我第一次使用数据库,所以如果这真的很明显,我深表歉意。

编辑:
在评论中出现的 DadOpc 类:
    public partial class DadOpc
{
public int Iddad { get; set; }
public string Computer { get; set; }
public string OpcserviceName { get; set; }
public float? ScanInterval { get; set; }
public byte? ServerType { get; set; }
public byte? UseSecurity { get; set; }

public virtual Dad IddadNavigation { get; set; }
}

编辑 2: (关于 jcruz 的解决方案)

错误信息:

System.InvalidOperationException: 'The child/dependent side could not be determined for the one-to-one relationship between 'DadMasCon.IddadNavigation' and 'Dad.DadMasCon'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.'



类爸:
public partial class Dad
{
public Dad()
{
DadCh = new HashSet<DadCh>();
DadConnection = new HashSet<DadConnection>();
}

//user added Id-Fields:
public int DadCmuId { get; set; }
public int DadOpcId { get; set; }
public int DadMasConId { get; set; }


public int Iddad { get; set; }
public string Name { get; set; }
public int? DadType { get; set; }
public short? Active { get; set; }
public DateTime? LastUpdate { get; set; }
public byte? SynchronizationStatus { get; set; }

public virtual DadCmu DadCmu { get; set; }
public virtual DadMasCon DadMasCon { get; set; }
public virtual DadOpc DadOpc { get; set; }
public virtual ICollection<DadCh> DadCh { get; set; }
public virtual ICollection<DadConnection> DadConnection { get; set; }
}

DadMasCon 类:
        public DadMasCon()
{
DadModbusRegConfig = new HashSet<DadModbusRegConfig>();
}

public int Iddad { get; set; }
public int UnitNo { get; set; }
public byte? Model { get; set; }
public DateTime? RefTimeOffset { get; set; }
public float? TimeoutComm { get; set; }
public int? Port { get; set; }
public float? ConnectionInterval { get; set; }
public byte? ExternalComm { get; set; }
public string Mvbparameters { get; set; }
public byte? ModbusBps { get; set; }
public byte? ModbusParity { get; set; }
public byte? ModbusStopBits { get; set; }
public byte? ModbusMode { get; set; }
public byte? ModbusSlaveAddress { get; set; }
public byte? CardType0 { get; set; }
public byte? CardType1 { get; set; }
public byte? CardType2 { get; set; }
public byte? CardType3 { get; set; }
public string LastIp { get; set; }
public string HiddenParameter { get; set; }
public int? SerialNo { get; set; }
public int? LastSequence { get; set; }
public int? TcppackageErrors { get; set; }
public byte? UsePrivateFirmware { get; set; }
public byte? ConfigurationLock { get; set; }
public byte? IecclientOn { get; set; }
public string IecclientAddress { get; set; }
public string IecclientDomain { get; set; }
public byte? IecsrvOn { get; set; }
public byte? IecsrvNrOfClents { get; set; }
public string IecsrvAuthentication { get; set; }
public string Iecmmsparam { get; set; }
public float? IecclientPollInterval { get; set; }
public byte? NtpserverType { get; set; }
public string Ntpipaddress { get; set; }
public string IecmmsparamFileName { get; set; }
public string ModbusTcpaddress { get; set; }
public int? ModbusTcpportNr { get; set; }
public byte? ModbusByteOrder { get; set; }
public byte? ModbusClientValueType { get; set; }
public string Macaddress { get; set; }
public byte? MonitorInitiate { get; set; }

public virtual Dad IddadNavigation { get; set; }
public virtual ICollection<DadModbusRegConfig> DadModbusRegConfig { get; set; }
}

OnModelCreating:
modelBuilder.Entity<DadMasCon>(entity =>
{
entity.HasKey(e => e.Iddad)
.HasName("PK_DadMasCon16");

entity.Property(e => e.Iddad)
.HasColumnName("IDDad")
.ValueGeneratedNever();

entity.Property(e => e.IecclientAddress).HasColumnName("IECClientAddress");

entity.Property(e => e.IecclientDomain).HasColumnName("IECClientDomain");

entity.Property(e => e.IecclientOn).HasColumnName("IECClientOn");

entity.Property(e => e.IecclientPollInterval).HasColumnName("IECClientPollInterval");

entity.Property(e => e.Iecmmsparam).HasColumnName("IECMMSParam");

entity.Property(e => e.IecmmsparamFileName).HasColumnName("IECMMSParamFileName");

entity.Property(e => e.IecsrvAuthentication).HasColumnName("IECSrvAuthentication");

entity.Property(e => e.IecsrvNrOfClents).HasColumnName("IECSrvNrOfClents");

entity.Property(e => e.IecsrvOn).HasColumnName("IECSrvOn");

entity.Property(e => e.Macaddress).HasColumnName("MACAddress");

entity.Property(e => e.ModbusTcpaddress)
.HasColumnName("ModbusTCPAddress")
.HasMaxLength(50);

entity.Property(e => e.ModbusTcpportNr).HasColumnName("ModbusTCPPortNr");

entity.Property(e => e.Mvbparameters).HasColumnName("MVBParameters");

entity.Property(e => e.Ntpipaddress).HasColumnName("NTPIPAddress");

entity.Property(e => e.NtpserverType).HasColumnName("NTPServerType");

entity.Property(e => e.RefTimeOffset).HasColumnType("datetime");

entity.Property(e => e.TcppackageErrors).HasColumnName("TCPPackageErrors");

//user generated:
entity.HasOne(d => d.IddadNavigation)
.WithOne(p => p.DadMasCon)
.HasForeignKey<Dad>(d => d.DadMasConId);

//original:
//entity.HasOne(d => d.IddadNavigation)
// .WithOne(p => p.DadMasCon)
// .HasForeignKey<DadMasCon>(d => d.Iddad)
// .HasConstraintName("FK_DadMasCon_Dad");
});

最佳答案

由于您正在尝试定义一对一关系,因此您需要在 Dad 上明确定义反向引用 ID。模型以确保它映射到单个 DadCmu记录。

public partial class Dad
{
public int DadCmuId { get; set; }
public virtual DadCmu DadCmu { get; set; }
}

然后,您可以使用 fluent API 进行配置,如下所示:
modelBuilder.Entity<DadCmu>(entity =>
{
entity.HasOne(d => d.IddadNavigation)
.WithOne(p => p.DadCmu)
.HasForeignKey<Dad>(d => d.DadCmuId);
});

HTH

关于c# - 如何正确定义 EF Core 创建的这些类之间的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59859422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com