项目有两个表。它从传感器到数据库取一个值。如何保存到具有关系的两个数据库?代码可能是错误的。也许有人可以帮忙。
public class Assets
    {
        public int id { get; set; }
        public int? sensorid { get; set; }
        public int? value { get; set; }
        public DateTime? DateTime { get; set; }
        public virtual Sensors Sensors { get; set; }
    }
和
 public class Sensors
{
    public int id { get; set; }
    public int? sensorname { get; set; }
    public virtual ICollection<Assets> Assets { get; set; }
}
控制者
 public ActionResult Index(Sensors sensors, Assets assets)
    {
        int[] response = client.ReadHoldingRegisters(StartAddress, quantity);
        client.Disconnect();
        Assets asst = new Assets();
        Sensors sns = new Sensors();
        for (int i = 0; i < quantity; i++)
        {
            asst.value = response[i];
            sns.id = StartAddress;
            db.Assets.Add(asst);
            for (int x = 0; x < 3; x++)
            {
                db.Sensors.Add(sns);
            }
            db.SaveChanges();
        }
                    版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
传感器已经存在
您不必再将Sensor添加到数据库中,只需添加一些资产,每个响应一个资产。
Asset.Value等于相应的响应值,并且每个资产都属于主键等于StartAddress的Sensor。外键SensorId引用此Sensor,因此SensorId需要设置为StartAddress。
int[] assetValues = client.ReadHoldingRegisters(startAddress, quantity);
// for every assetValue create an Asset with Value equal to assetValue
// and a foreign key equal to startAddress:
var assetsToAdd = assetValues
    .Select(assetValue => new Asset()
    {
        Value = assetValue,
        SensorId = startAddress,
    });
using (var dbContext = new MyDbContext(...))
{
    // Add the Assets in one call to DbSet.AddRange(IEnumerable)
    dbContext.Assets.AddRange(assetsToAdd);
    // and save the changes:
    dbContext.SaveChanges();
}
 
如果对序列使用正确的标识符和正确的复数,对单个项目使用正确的复数,您是否看到代码容易得多?
传感器尚不存在
在这种情况下,您必须添加一个传感器。您无法分配主键,因此无法填写Asset.SensorId。幸运的是,实体框架足够聪明,可以为您解决此问题:
int[] assetValues = client.ReadHoldingRegisters(startAddress, quantity);
using (var dbContext = new MyDbContext(...))
{
    // Add one Sensor with all its Assets in one go
    dbContext.Sensors.Add(new Sensor()
    {
        // fill the Sensor properties that you know.
        // don't fill the Id
        ...
        Assets = assetValues
                 .Select(assetValue => new Asset()
                 {
                      Value = assetValue,
                      // no need to fill SensorId, Entity framework will do that for you
                      ... // if needed: fill other Asset properties
                 })
                 .ToList(),
    });
    dbContext.SaveChanges(); 
}
 
如果需要,可以分两个步骤进行:
using (var dbContext = new MyDbContext(...))
{
    // Add one Sensor without Assets
    var addedSensor = dbContext.Sensors.Add(new Sensor()
    {
        // fill the Sensor properties that you know.
        // don't fill the Id, nor property Assets
        ...
    });
    // Add the Assets in one call to DbSet.AddRange(IEnumerable)
    dbContext.Assets.AddRange(assetValues.Select(assetValue => new Asset()
                     {
                         Value = assetValue,
                         Sensor = addedSensor,
                     }));
    dbContext.SaveChanges(); 
}