Ich habe Probleme beim Einfügen von Daten in Datenbanktabelle, die Fremdschlüssel hat. Als Fehler Ich erhalteEF Core expliziter Wert für Identitätsspalte
Kann nicht expliziten Wert für Identitätsspalte in Tabelle einfügen ‚MachineTypes‘, wenn IDENTITY_INSERT auf OFF.Cannot gesetzt expliziten Wert für Identitätsspalte in Tabelle einfügen ‚SpareTypes‘ wenn IDENTITY ist auf AUS gestellt.
BaseEntity.cs
public abstract class BaseEntity
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Int64 Id { get; set; }
public DateTime CreateDate { get; set; }
}
MachineType.cs
public class MachineType : BaseEntity
{
[Required]
[StringLength(50)]
public string Name { get; set; }
}
SpareType.cs
public class SpareType : BaseEntity
{
[Required]
[StringLength(25)]
public string Name { get; set; }
}
SparePart.cs
public class SparePart : BaseEntity
{
[Required]
[StringLength(100)]
public string InternalCode { get; set; }
[StringLength(4096)]
public string Description { get; set; }
[StringLength(255)]
public string NameOnFolder { get; set; }
public decimal? Enter { get; set; }
public decimal? Exit { get; set; }
public decimal? Thickness { get; set; }
public string Band { get; set; }
public string Color { get; set; }
public bool Elastic { get; set; }
[Required]
public virtual MachineType MachineType { get; set; }
[Required]
public virtual SpareType SpareType { get; set; }
}
SparePartViewModel.cs
public class SparePartViewModel
{
public int Id { get; set; }
public DateTime CreateDate { get; set; }
[Required]
[StringLength(100)]
public string InternalCode { get; set; }
[StringLength(4096)]
public string Description { get; set; }
[StringLength(255)]
public string NameOnFolder { get; set; }
public decimal? Enter { get; set; }
public decimal? Exit { get; set; }
public decimal? Thickness { get; set; }
public string Band { get; set; }
public string Color { get; set; }
public bool Elastic { get; set; }
[Required]
public virtual MachineType MachineType { get; set; }
[Required]
public virtual SpareType SpareType { get; set; }
}
Controller für
public class AppContext : IdentityDbContext<ApplicationUser>
{
private IConfigurationRoot _config;
public AppContext(IConfigurationRoot config, DbContextOptions options) : base(options)
{
_config = config;
}
public DbSet<SpareType> SpareTypes { get; set; }
public DbSet<MachineType> MachineTypes { get; set; }
public DbSet<SparePart> SpareParts { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<SpareType>()
.HasIndex(s => new { s.Name })
.IsUnique(true);
builder.Entity<MachineType>()
.HasIndex(s => s.Name)
.IsUnique(true);
base.OnModelCreating(builder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(_config["ConnectionStrings:RndDbContextConnection"]);
}
}
Während der Buchung aller Daten sind korrekt fangen
[Route("/api/v1/items")]
public class SparePartController : Controller
{
private IRepository<SparePart> _repoSparePart;
public SparePartController(IRepository<SparePart> repoSparePart)
{
_repoSparePart = repoSparePart;
}
[HttpPost("")]
public async Task<IActionResult> Post([FromBody]SparePartViewModel viewModel)
{
if (ModelState.IsValid)
{
var newItem = Mapper.Map<SparePart>(viewModel);
newItem.CreateDate = DateTime.Now;
_repoSparePart.Insert(newItem);
if (await _repoSparePart.SaveChangesAsync())
{
return Created($"items/{newItem.InternalCode}", Mapper.Map<SparePartViewModel>(viewModel));
}
}
return BadRequest("Failed to save.");
}
}
AppContext.cs Posting, aber wann sollte ins sein? In Datenbank eingefügt bekomme ich einen Fehler.
Vielen Dank. Mit tiefer und langer Untersuchung fand ich das auch. –