entity-framework-core

EF Core always create .Annotation(“SqlServer:Identity”, “1, 1”) on add-migration

主宰稳场 提交于 2021-02-11 06:16:33
问题 I'm having a problem with Migration EF Core. To make the scenario clear, I'm assigning it to an existing database, which is intended to use migration for database control from now on. But I am having difficulties. What I have done so far. I generated the scaffolding from the existing database. I added the migration, so it generated all the "Up" for database creation. In a clean database, ran update-database. So far perfect, everything worked as expected. But from this step every time I

EF Core always create .Annotation(“SqlServer:Identity”, “1, 1”) on add-migration

不打扰是莪最后的温柔 提交于 2021-02-11 06:10:20
问题 I'm having a problem with Migration EF Core. To make the scenario clear, I'm assigning it to an existing database, which is intended to use migration for database control from now on. But I am having difficulties. What I have done so far. I generated the scaffolding from the existing database. I added the migration, so it generated all the "Up" for database creation. In a clean database, ran update-database. So far perfect, everything worked as expected. But from this step every time I

Updating many to many in Entity Framework core

一曲冷凌霜 提交于 2021-02-10 19:23:37
问题 Here are the relevant classes and their configuration: public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductId { get; set; } [Required] public string ProductName { get; set; } public ICollection<ProductSpecification> ProductSpecifications { get; set; } } public class ProductAttributeValue { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductAttributeValueId { get; set; } [Required] public string

Updating many to many in Entity Framework core

吃可爱长大的小学妹 提交于 2021-02-10 19:22:35
问题 Here are the relevant classes and their configuration: public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductId { get; set; } [Required] public string ProductName { get; set; } public ICollection<ProductSpecification> ProductSpecifications { get; set; } } public class ProductAttributeValue { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductAttributeValueId { get; set; } [Required] public string

Updating many to many in Entity Framework core

老子叫甜甜 提交于 2021-02-10 19:21:49
问题 Here are the relevant classes and their configuration: public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductId { get; set; } [Required] public string ProductName { get; set; } public ICollection<ProductSpecification> ProductSpecifications { get; set; } } public class ProductAttributeValue { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductAttributeValueId { get; set; } [Required] public string

Updating many to many in Entity Framework core

北慕城南 提交于 2021-02-10 19:21:14
问题 Here are the relevant classes and their configuration: public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductId { get; set; } [Required] public string ProductName { get; set; } public ICollection<ProductSpecification> ProductSpecifications { get; set; } } public class ProductAttributeValue { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ProductAttributeValueId { get; set; } [Required] public string

EF Core - error with duplicated key while adding data with unique index

你。 提交于 2021-02-10 18:51:54
问题 I'm trying to add data to the database using EF Core, but I cannot overcome duplicate key error: Cannot insert duplicate key row in object 'dbo.Stocks' with unique index 'IX_Stocks_Name'. The duplicate key value is (stock1). I have entity Stock that has relation one-to-many with Transaction - one Stock can be related with many Transaction s. The problem doesn't occur only if there's no Stock with a given ID in the database - in that case I can add many Transaction s with the same StockID and

Can't update value: 'Primary Key' has a temporary value while attempting to change the entity's state to 'Modified'

…衆ロ難τιáo~ 提交于 2021-02-10 14:44:39
问题 This is my first ASP .Net Core project. It will hold directors. Each director has a page that shows a list of his/her movies. I have two classes. Movie : public class Movie { public int MovieId { get; private set; } public int DirectorId { get; set; } [Required] public string Title { get; set; } public string Year { get; set; } public string Description { get; set; } } And Director : public class Director { public Director() { Movies = new List<Movie>(); } public int DirectorId { get; private

How can i create a multi level menu in MVC core?

守給你的承諾、 提交于 2021-02-10 14:33:19
问题 The HTML menu rendered from my helper function is Category 1 Category 2 Category 2 Category 3 Category 4 I have set Category 2 as a child category of Category 1 . Unfortunately, the current HTML helper displays again Category 2 as a parent afterwards. Id ParentCategoryId Description 1 NULL Category 1 2 1 Category 2 3 NULL Category 3 4 NULL Category 4 5 NULL Category 5 How should I modify my helper function? @{ Func<dynamic, IHtmlContent> ShowMenu(List<Category> cats) => @<ul> @foreach (var

EF Core model building conventions

风流意气都作罢 提交于 2021-02-10 06:27:09
问题 In EF6 it was possible to define conventions based on property types during model building, like so... public interface IEntity { Guid Id { get; } } public class MyEntity : IEntity { public Guid Id { get; set; } } public class MyDbContext : DbContext { public override void OnModelCreating(DbModelBuilder builder) { builder .Properties<Guid>() .Where(x => x.Name == nameof(IEntity.Id) .Configure(a=>a.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)); } } This approach could also be