Вопрос по entity-framework-4, .net, c# – Entity Framework Code First AddOrUpdate метод вставки повторяющихся значений

53

Error: User Rate Limit Exceeded

public class Hall
{
    [Key]
    public int Id {get; set;}

    public string Name [get; set;}
}

Error: User Rate Limit ExceededSeedError: User Rate Limit ExceededAddOrUpdateError: User Rate Limit Exceeded

var hall1 = new Hall { Name = "French" };
var hall2 = new Hall { Name = "German" };
var hall3 = new Hall { Name = "Japanese" };

context.Halls.AddOrUpdate(
    h => h.Name,
    hall1,
    hall2,
    hall3
);

Error: User Rate Limit Exceeded

Add-Migration Current
Update-Database

Error: User Rate Limit ExceededUpdate-DatabaseError: User Rate Limit Exceeded

Id  Name
1   French
2   Japaneese
3   German
4   French
5   Japanese

Error: User Rate Limit ExceededIdError: User Rate Limit ExceededNameError: User Rate Limit Exceeded

UPDATE:

Error: User Rate Limit Exceeded

var hall1 = new Hall { Id = 1, Name = "French" };
var hall2 = new Hall { Id = 2, Name = "German" };
var hall3 = new Hall { Id = 3, Name = "Japanese" };

context.Halls.AddOrUpdate(
                h => h.Id,
                hall1);

context.Halls.AddOrUpdate(
                h => h.Id,
                hall2);

context.Halls.AddOrUpdate(
                h => h.Id,
                hall3);

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded firecape
Error: User Rate Limit Exceeded Y.Yanavichus
Error: User Rate Limit Exceededthedatafarm.com/blog/data-access/… Kirin Yao
Error: User Rate Limit Exceeded Y.Yanavichus
Error: User Rate Limit ExceededAddOrUpdateError: User Rate Limit Exceeded Ladislav Mrnka

Ваш Ответ

12   ответов
0

DBCC CHECKIDENT (tableName, RESEED, 0)
61

new Hall {...}

protected override void Seed(HallContext context)
{
    context.Halls.AddOrUpdate(
        h => h.Name,   // Use Name (or some other unique field) instead of Id
        new Hall
        {
            Name = "Hall 1"
        },
        new Hall
        {
            Name = "Hall 2"
        });

    context.SaveChanges();
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededstackoverflow.com/questions/35452497/…
0

9

public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

protected override void Seed(HallContext context)
{
    context.Halls.AddOrUpdate(
        h => h.Id,
        new Hall
        {
            Id = 1,
            Name = "Hall 1"
        },
        new Hall
        {
            Id = 2,
            Name = "Hall 2"
        });

    context.SaveChanges();
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Y.Yanavichus
0

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Y.Yanavichus
Error: User Rate Limit Exceeded Y.Yanavichus
1

AddOrUpdatecontext.Halls.AddOrUpdate(h => h.Name, hall1, hall2, hall3)

0

public class ApplicationContext : DbContext, IDbContext
    {
        public ApplicationContext() : base("ApplicationContext")
        {
             
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            Database.SetInitializer<ApplicationContext>(null);
            base.OnModelCreating(modelBuilder);
        }
     }

2
This worked for me Delete all the rows in the table. Reset the incremental identity to 0. DBCC CHECKIDENT (yourtablename, RESEED, 0) (The primary keys specified in the Seed() must match those in the database table so that they do not duplicate.) Specify the primary keys in the 'seed' method. Run the Seed() method several times and you check if they duplicated.
Error: User Rate Limit Exceeded
-1

 context.Halls.AddOrUpdate(new Hall[]{hall1,hall2, hall3});
9

modelBuilder.Entity<MyClass>().Property(p => p.Id)
    .HasDatabaseGeneratedOption(System.ComponentModel
    .DataAnnotations.Schema.DatabaseGeneratedOption.None); 

Context.MyClasses.Max(c=>c.Id) + 1

0

public class Hall
{
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id {get; set;}

    public string Name [get; set;}
 }
0

Error: User Rate Limit Exceeded Y.Yanavichus

Похожие вопросы