开发者社区> 问答> 正文

MVC保存到两个表

项目有两个表。它从传感器到数据库取一个值。如何保存到具有关系的两个数据库?代码可能是错误的。也许有人可以帮忙。

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();
        }

展开
收起
祖安文状元 2020-01-03 16:16:53 488 0
1 条回答
写回答
取消 提交回答
  • 传感器已经存在

    您不必再将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(); 
    }
    
    2020-01-03 16:17:40
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载