实体框架核心放置操作不起作用

编程入门 行业动态 更新时间:2024-10-23 21:33:48
本文介绍了实体框架核心放置操作不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我得到

未处理的异常错误:实体类型车辆"上的属性"Id"是键的一部分,因此无法进行修改或将其标记为已修改.要使用标识的外键更改现有实体的主体,请先删除该依赖项,然后调用"SaveChanges",然后将该依赖项与新的主体相关联.实体类型"Vehicle"的属性"Id"是密钥的一部分,因此无法进行修改或标记为已修改.要使用标识的外键更改现有实体的委托人,请先删除依赖项,然后调用"SaveChanges",然后将依赖项与新委托人关联起来.

Unhandled Exception Error: the property 'Id' on entity type 'Vehicle' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principalThe property 'Id' on entity type 'Vehicle' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal

这是我的Put API:

Here is my Put API:

[HttpPut("{id}")] public IActionResult UpdateVehicle(int id, [FromBody] SaveVehicleResource vehicleResource) { if (!ModelState.IsValid) return BadRequest(ModelState); var vehicle = context.Vehicles.Include(v => v.Features).SingleOrDefault(v => v.Id == id); if (vehicle == null) return NotFound(); mapper.Map(vehicleResource, vehicle); vehicle.LastUpdate = DateTime.Now; context.SaveChanges(); var result = mapper.Map<Vehicle, SaveVehicleResource>(vehicle); return Ok(result); }

这里是 DbContext :

public class VegaDbContext : DbContext { public DbSet<Make> Makes { get; set; } public DbSet<Feature> Features { get; set; } public DbSet<Vehicle> Vehicles { get; set; } public DbSet<Model> Models { get; set; } public VegaDbContext(DbContextOptions<VegaDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<VehicleFeature>().HasKey(vf => new { vf.VehicleId, vf.FeatureId }); } }

车辆类:

public class Vehicle { public int Id { get; set; } public int ModelId { get; set; } public Model Model { get; set; } public bool IsRegistered { get; set; } public Contact Contact { get; set; } public ICollection<VehicleFeature> Features { get; set; } public DateTime LastUpdate { get; set; } public Vehicle() { Features = new Collection<VehicleFeature>(); } }

VehicleFeature 类:

public class VehicleFeature { public int VehicleId { get; set; } public int FeatureId { get; set; } public Vehicle Vehicle { get; set; } public Feature Feature { get; set; } }

推荐答案

免责声明:看起来好像您正在使用AutoMapper.因此,让我们看一下您的AutoMapper配置.具体来说,看看是否可以在其中找到类似 .CreateMap< Vehicle,SaveVehicleResource> 的东西.

Disclaimer: it really looks like you're using AutoMapper. So let's take a look at your AutoMapper configuration. Specifically, see if you can find something like .CreateMap<Vehicle, SaveVehicleResource> in there.

这两种情况之一正在发生:

  • 您的AutoMapper配置为针对这些类显式地 CreateMap ,并且包含类似于 .ForMember(x => x.Id,x.MapFrom(y => y)的语句..Id))
  • 您的AutoMapper未明确配置 ,这意味着它正在查找属性 .Id ,因为两个类都使用相同的名称对其进行了定义.您必须明确忽略该成员.
  • Your AutoMapper is configured to explicitly CreateMap for these classes and it includes a statement similar to .ForMember(x => x.Id, x.MapFrom(y => y.Id))
  • Your AutoMapper is not configured explicitly which means it is finding the property .Id because both classes define it with the same name. You must explicitly ignore that member.
  • 无论发生了哪些事情,您都必须告诉AutoMapper忽略该属性.

    Regardless which of those things has happened, you'll have to tell AutoMapper to ignore that property.

    CreateMap<Vehicle, SaveVehicleResource>(...) .ForMember(x => x.Id, y => y.Ignore());

    更多推荐

    实体框架核心放置操作不起作用

    本文发布于:2023-11-16 10:11:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1603345.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:实体   框架   不起作用   核心   操作

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!