How do I seed the master data during “OnModelCreating” method of DbContext in case of auto generated...












0















Application Framework: ASPNET Core 2.1



This is my class



public class Employee
{
[DataMember(Name = "key")]
[Key]
public Guid Key { get; set; }
[Required]
[DataMember(Name = "Name")]
public string Name { get; set; }
}


I have below code which enable the seeding of data and auto generation of Guid column



    protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Auto generation of Guid
modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()");

//seeding of data
modelBuilder.Entity<Employee>().HasData(
new Employee{ Name = "Hamlet" },
new Employee{ Name = "King Lear" },
new Employee{ Name = "Othello" }
);
}


I get the below error while running the add-migration InitialCreate command




The seed entity for entity type 'Employee' cannot be added because there was no value provided for the required property 'Key'.




I do not want to send the value for Key. I want it to be auto-generated in SQL Server database.



My basis requirement is : Insert master data (updated) whenever I run update-database in the package manager console.










share|improve this question

























  • So far, the HasData method doesn't have an overload that accepts a matching key specification, as AddOrUpdate has. It could be a feature request. Until then your only option is to specify the key value. Note however, that you're always going to need the specified key if you also want to seed data referring to Employee.

    – Gert Arnold
    Nov 24 '18 at 19:59


















0















Application Framework: ASPNET Core 2.1



This is my class



public class Employee
{
[DataMember(Name = "key")]
[Key]
public Guid Key { get; set; }
[Required]
[DataMember(Name = "Name")]
public string Name { get; set; }
}


I have below code which enable the seeding of data and auto generation of Guid column



    protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Auto generation of Guid
modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()");

//seeding of data
modelBuilder.Entity<Employee>().HasData(
new Employee{ Name = "Hamlet" },
new Employee{ Name = "King Lear" },
new Employee{ Name = "Othello" }
);
}


I get the below error while running the add-migration InitialCreate command




The seed entity for entity type 'Employee' cannot be added because there was no value provided for the required property 'Key'.




I do not want to send the value for Key. I want it to be auto-generated in SQL Server database.



My basis requirement is : Insert master data (updated) whenever I run update-database in the package manager console.










share|improve this question

























  • So far, the HasData method doesn't have an overload that accepts a matching key specification, as AddOrUpdate has. It could be a feature request. Until then your only option is to specify the key value. Note however, that you're always going to need the specified key if you also want to seed data referring to Employee.

    – Gert Arnold
    Nov 24 '18 at 19:59
















0












0








0








Application Framework: ASPNET Core 2.1



This is my class



public class Employee
{
[DataMember(Name = "key")]
[Key]
public Guid Key { get; set; }
[Required]
[DataMember(Name = "Name")]
public string Name { get; set; }
}


I have below code which enable the seeding of data and auto generation of Guid column



    protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Auto generation of Guid
modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()");

//seeding of data
modelBuilder.Entity<Employee>().HasData(
new Employee{ Name = "Hamlet" },
new Employee{ Name = "King Lear" },
new Employee{ Name = "Othello" }
);
}


I get the below error while running the add-migration InitialCreate command




The seed entity for entity type 'Employee' cannot be added because there was no value provided for the required property 'Key'.




I do not want to send the value for Key. I want it to be auto-generated in SQL Server database.



My basis requirement is : Insert master data (updated) whenever I run update-database in the package manager console.










share|improve this question
















Application Framework: ASPNET Core 2.1



This is my class



public class Employee
{
[DataMember(Name = "key")]
[Key]
public Guid Key { get; set; }
[Required]
[DataMember(Name = "Name")]
public string Name { get; set; }
}


I have below code which enable the seeding of data and auto generation of Guid column



    protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Auto generation of Guid
modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()");

//seeding of data
modelBuilder.Entity<Employee>().HasData(
new Employee{ Name = "Hamlet" },
new Employee{ Name = "King Lear" },
new Employee{ Name = "Othello" }
);
}


I get the below error while running the add-migration InitialCreate command




The seed entity for entity type 'Employee' cannot be added because there was no value provided for the required property 'Key'.




I do not want to send the value for Key. I want it to be auto-generated in SQL Server database.



My basis requirement is : Insert master data (updated) whenever I run update-database in the package manager console.







c# sql-server entity-framework asp.net-core database-migration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 16:49







kudlatiger

















asked Nov 24 '18 at 16:15









kudlatigerkudlatiger

85511134




85511134













  • So far, the HasData method doesn't have an overload that accepts a matching key specification, as AddOrUpdate has. It could be a feature request. Until then your only option is to specify the key value. Note however, that you're always going to need the specified key if you also want to seed data referring to Employee.

    – Gert Arnold
    Nov 24 '18 at 19:59





















  • So far, the HasData method doesn't have an overload that accepts a matching key specification, as AddOrUpdate has. It could be a feature request. Until then your only option is to specify the key value. Note however, that you're always going to need the specified key if you also want to seed data referring to Employee.

    – Gert Arnold
    Nov 24 '18 at 19:59



















So far, the HasData method doesn't have an overload that accepts a matching key specification, as AddOrUpdate has. It could be a feature request. Until then your only option is to specify the key value. Note however, that you're always going to need the specified key if you also want to seed data referring to Employee.

– Gert Arnold
Nov 24 '18 at 19:59







So far, the HasData method doesn't have an overload that accepts a matching key specification, as AddOrUpdate has. It could be a feature request. Until then your only option is to specify the key value. Note however, that you're always going to need the specified key if you also want to seed data referring to Employee.

– Gert Arnold
Nov 24 '18 at 19:59














1 Answer
1






active

oldest

votes


















1














Try adding the following attribute to the Key property:.



[DatabaseGenerated(DatabaseGeneratedOption.Identity)]


This will instruct the DB to autogenerate the Key when none is specified



Update



Apparently you have to always specify the ID even for autogenerated for the data seeded in migrations, because it is used to match if the given entry is already present in the database.



However, a solution can be to manually insert the needed records in the database after it is initialized (after you call context.Database.Migrate() for example). Of course here you have to manually check if the given record is not already there.






share|improve this answer


























  • not working. it show same error, I already have modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()"); for auto generation. 'add-migration' works if I remove the seed data code. so what is the alternative option to seed data?

    – kudlatiger
    Nov 24 '18 at 16:29













  • I have updated my answer with my finidng

    – Martin Zikmund
    Nov 24 '18 at 16:33











  • It resolved the issue but did not solve the need. It's inserting the key's which I have specified as you suggested. I wish to see auto generated guids in the database.

    – kudlatiger
    Nov 24 '18 at 16:44











  • If you combine the updated part of my question as well as the [DatabaseGenerated(Identity)] attribute, it should work

    – Martin Zikmund
    Nov 24 '18 at 18:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460030%2fhow-do-i-seed-the-master-data-during-onmodelcreating-method-of-dbcontext-in-ca%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try adding the following attribute to the Key property:.



[DatabaseGenerated(DatabaseGeneratedOption.Identity)]


This will instruct the DB to autogenerate the Key when none is specified



Update



Apparently you have to always specify the ID even for autogenerated for the data seeded in migrations, because it is used to match if the given entry is already present in the database.



However, a solution can be to manually insert the needed records in the database after it is initialized (after you call context.Database.Migrate() for example). Of course here you have to manually check if the given record is not already there.






share|improve this answer


























  • not working. it show same error, I already have modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()"); for auto generation. 'add-migration' works if I remove the seed data code. so what is the alternative option to seed data?

    – kudlatiger
    Nov 24 '18 at 16:29













  • I have updated my answer with my finidng

    – Martin Zikmund
    Nov 24 '18 at 16:33











  • It resolved the issue but did not solve the need. It's inserting the key's which I have specified as you suggested. I wish to see auto generated guids in the database.

    – kudlatiger
    Nov 24 '18 at 16:44











  • If you combine the updated part of my question as well as the [DatabaseGenerated(Identity)] attribute, it should work

    – Martin Zikmund
    Nov 24 '18 at 18:38
















1














Try adding the following attribute to the Key property:.



[DatabaseGenerated(DatabaseGeneratedOption.Identity)]


This will instruct the DB to autogenerate the Key when none is specified



Update



Apparently you have to always specify the ID even for autogenerated for the data seeded in migrations, because it is used to match if the given entry is already present in the database.



However, a solution can be to manually insert the needed records in the database after it is initialized (after you call context.Database.Migrate() for example). Of course here you have to manually check if the given record is not already there.






share|improve this answer


























  • not working. it show same error, I already have modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()"); for auto generation. 'add-migration' works if I remove the seed data code. so what is the alternative option to seed data?

    – kudlatiger
    Nov 24 '18 at 16:29













  • I have updated my answer with my finidng

    – Martin Zikmund
    Nov 24 '18 at 16:33











  • It resolved the issue but did not solve the need. It's inserting the key's which I have specified as you suggested. I wish to see auto generated guids in the database.

    – kudlatiger
    Nov 24 '18 at 16:44











  • If you combine the updated part of my question as well as the [DatabaseGenerated(Identity)] attribute, it should work

    – Martin Zikmund
    Nov 24 '18 at 18:38














1












1








1







Try adding the following attribute to the Key property:.



[DatabaseGenerated(DatabaseGeneratedOption.Identity)]


This will instruct the DB to autogenerate the Key when none is specified



Update



Apparently you have to always specify the ID even for autogenerated for the data seeded in migrations, because it is used to match if the given entry is already present in the database.



However, a solution can be to manually insert the needed records in the database after it is initialized (after you call context.Database.Migrate() for example). Of course here you have to manually check if the given record is not already there.






share|improve this answer















Try adding the following attribute to the Key property:.



[DatabaseGenerated(DatabaseGeneratedOption.Identity)]


This will instruct the DB to autogenerate the Key when none is specified



Update



Apparently you have to always specify the ID even for autogenerated for the data seeded in migrations, because it is used to match if the given entry is already present in the database.



However, a solution can be to manually insert the needed records in the database after it is initialized (after you call context.Database.Migrate() for example). Of course here you have to manually check if the given record is not already there.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 16:32

























answered Nov 24 '18 at 16:26









Martin ZikmundMartin Zikmund

24.1k63460




24.1k63460













  • not working. it show same error, I already have modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()"); for auto generation. 'add-migration' works if I remove the seed data code. so what is the alternative option to seed data?

    – kudlatiger
    Nov 24 '18 at 16:29













  • I have updated my answer with my finidng

    – Martin Zikmund
    Nov 24 '18 at 16:33











  • It resolved the issue but did not solve the need. It's inserting the key's which I have specified as you suggested. I wish to see auto generated guids in the database.

    – kudlatiger
    Nov 24 '18 at 16:44











  • If you combine the updated part of my question as well as the [DatabaseGenerated(Identity)] attribute, it should work

    – Martin Zikmund
    Nov 24 '18 at 18:38



















  • not working. it show same error, I already have modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()"); for auto generation. 'add-migration' works if I remove the seed data code. so what is the alternative option to seed data?

    – kudlatiger
    Nov 24 '18 at 16:29













  • I have updated my answer with my finidng

    – Martin Zikmund
    Nov 24 '18 at 16:33











  • It resolved the issue but did not solve the need. It's inserting the key's which I have specified as you suggested. I wish to see auto generated guids in the database.

    – kudlatiger
    Nov 24 '18 at 16:44











  • If you combine the updated part of my question as well as the [DatabaseGenerated(Identity)] attribute, it should work

    – Martin Zikmund
    Nov 24 '18 at 18:38

















not working. it show same error, I already have modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()"); for auto generation. 'add-migration' works if I remove the seed data code. so what is the alternative option to seed data?

– kudlatiger
Nov 24 '18 at 16:29







not working. it show same error, I already have modelBuilder.Entity<Employee>().Property(x => x.Key).HasDefaultValueSql("NEWID()"); for auto generation. 'add-migration' works if I remove the seed data code. so what is the alternative option to seed data?

– kudlatiger
Nov 24 '18 at 16:29















I have updated my answer with my finidng

– Martin Zikmund
Nov 24 '18 at 16:33





I have updated my answer with my finidng

– Martin Zikmund
Nov 24 '18 at 16:33













It resolved the issue but did not solve the need. It's inserting the key's which I have specified as you suggested. I wish to see auto generated guids in the database.

– kudlatiger
Nov 24 '18 at 16:44





It resolved the issue but did not solve the need. It's inserting the key's which I have specified as you suggested. I wish to see auto generated guids in the database.

– kudlatiger
Nov 24 '18 at 16:44













If you combine the updated part of my question as well as the [DatabaseGenerated(Identity)] attribute, it should work

– Martin Zikmund
Nov 24 '18 at 18:38





If you combine the updated part of my question as well as the [DatabaseGenerated(Identity)] attribute, it should work

– Martin Zikmund
Nov 24 '18 at 18:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460030%2fhow-do-i-seed-the-master-data-during-onmodelcreating-method-of-dbcontext-in-ca%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)