0

I have a simple Entity Class as under:

[Table("Grade")]
public class Grade
{
    //Tried changing this annotation to Identity
    //[DatabaseGenerated(DatabaseGeneratedOption.None), Key]
    [Key]
    public int GradeId { get; set; }
    public string GradeName { get; set; }
}

I am trying to seed this with EF7 and CSVHelper for DNX 5 project I am working, as under

using (StreamReader reader = new StreamReader(fs, System.Text.Encoding.UTF8, false)){
CsvReader csvReader = new CsvReader(reader);
IEnumerable<Grade> grades = csvReader.GetRecords<Grade>();

using (_context.Database.BeginTransaction()) 
{
_context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].[Grade] ON");
_context.Grades.AddRange(grades);
_context.SaveChanges();
_context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].[Grade] OFF");
}}

If I comment the using (_context.Database.BeginTransaction()) line error for IDENTITY_INSERT OFF appears though code gets compiled

However with the BeginTransaction() the following error appears

enter image description here

CSV File used for this project (CSVReader is working fine- returning all columns and data as shown under)

enter image description here

enter image description here

What could I be doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
NBaua
  • 583
  • 4
  • 16
  • 33
  • is this code first? it appears the column GradeName is integer type in the database. – DevilSuichiro Jun 08 '16 at 18:05
  • Nope, The GradeName is nvarchar, I checked. – NBaua Jun 09 '16 at 05:15
  • After adding following code error is gone, but nothing is seeded.. no error nothing working, debug shows data from CSV is properly fetched. protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasKey(a => a.GradeId); base.OnModelCreating(modelBuilder); } – NBaua Jun 09 '16 at 05:18
  • An addendum to this question, provided as an answer below, was deleted yesterday. If you still want an answer to this old question, or you think it could still be useful to the community, please edit your question with the new information. Alternatively please consider deleting the question. – halfer May 01 '17 at 07:59

0 Answers0