-1

I want to insert a new entry into my database with the following commands:

Repository<Fueltype, TankeeContext> repository = new Repository<Fueltype, TankeeContext>();
Fueltype fuel = new Fueltype() {
    Name = "Strom"
};
repository.Create(fuel);

That is the create- method of the repository

 public void Create(T entity)
 {
     using (Ctx db = new Ctx())
      {
         db.Set<T>().Add(entity);
         db.SaveChanges();
      }
 }

My problem is, that after inserting the entity Fueltype and calling the SaveChanges() method, the ID (IdFuelType) stays null, although it should be updated by the entity framework. My Entity Fueltype looks like that:

public partial class Fueltype
    {
        public Fueltype()
        {
            Gasprice = new HashSet<Gasprice>();
        }

        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int IdFuelType { get; set; }
        public string Name { get; set; }

        public ICollection<Gasprice> Gasprice { get; set; }
    }
Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
romaneso
  • 1,097
  • 1
  • 10
  • 26
  • Did you set the identity specification to true? – Tvt Oct 30 '17 at 08:50
  • Because I used Database-First, the entities got created automatically, so I am not quite sure. – romaneso Oct 30 '17 at 08:52
  • I would start bij checking that :) By default it's set to false. – Tvt Oct 30 '17 at 08:54
  • If it's a primary key it can't be null. Primary keys are non-nullable by definition. – Zohar Peled Oct 30 '17 at 08:55
  • @ZoharPeled I am aware of that, my primary key gets updated in the database, so when I call SaveChanges(), the entry gets inserted into the database (for example with id 40), but it doesn't get updated as a entity after calling that method. – romaneso Oct 30 '17 at 08:57
  • @Tvt I am using MySQL, so I guess there is no Identity Specification method, or? – romaneso Oct 30 '17 at 08:59
  • @romaneso hmm no idea then. I know if you use Model first for example on a newly created table the identity specification will be false and because of that it will not auto increment and leave the value "NULL" – Tvt Oct 30 '17 at 09:02
  • @Tvt yeah, I know - that's the first time that .net core and entity framework is doing funny things to me. – romaneso Oct 30 '17 at 09:04
  • What does `DatabaseGeneratedOption.None` mean? https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.databasegeneratedoption(v=vs.110).aspx – mjwills Oct 30 '17 at 09:14
  • @mjwills that doesn't work when you work by Database First unfortunately. – romaneso Oct 30 '17 at 09:47
  • Instead of DatabaseGeneratedOption.None, have you tried DatabaseGeneratedOption.Identity? – Mikkel Oct 30 '17 at 09:51
  • Yes, I have- didn't work for me either :( – romaneso Oct 30 '17 at 10:49
  • Change this line [DatabaseGenerated(DatabaseGeneratedOption.None)] to [DatabaseGenerated(DatabaseGeneratedOption.Identity)] – Sandeep Oct 30 '17 at 11:49

1 Answers1

1

You should set the value of IdFuelType or change this line of code

[DatabaseGenerated(DatabaseGeneratedOption.None)] to

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

Refer the following link

https://forums.asp.net/t/2080959.aspx?+Errors+DatabaseGenerated+DatabaseGeneratedOption+None+v+Key

Sandeep
  • 413
  • 4
  • 13