Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Entity framework

Entity framework

Scheduled Pinned Locked Moved Database
databasesales
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    columbos14927
    wrote on last edited by
    #1

    Hello, I am a new in EF. I have a Customewr class:

    public class Customer
    {
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    }

    And here is my DbContext class:

    public class DiveShopContext:DbContext
    {
    public DbSet<Customer> Customers { get; set; }
    }

    I want to be able to save Customers to my DB, i do it as follow:
    <pre lang="cs">class Program
    {
    static void Main(string[] args)
    {
    using (var db = new DiveShopContext())
    {

           var customer = new Customer();
           customer.CustomerId = 3045;
           customer.Name = &quot;Alex&quot;;
    
           db.Customers.Add(customer);
           db.SaveChanges();
       }
    }
    

    }</pre>

    Every thing is saved fine but the CustomerId is changed from 3045 to 1 and saved.

    J S 2 Replies Last reply
    0
    • C columbos14927

      Hello, I am a new in EF. I have a Customewr class:

      public class Customer
      {
      public int CustomerId { get; set; }
      public string Name { get; set; }
      public string Email { get; set; }
      }

      And here is my DbContext class:

      public class DiveShopContext:DbContext
      {
      public DbSet<Customer> Customers { get; set; }
      }

      I want to be able to save Customers to my DB, i do it as follow:
      <pre lang="cs">class Program
      {
      static void Main(string[] args)
      {
      using (var db = new DiveShopContext())
      {

             var customer = new Customer();
             customer.CustomerId = 3045;
             customer.Name = &quot;Alex&quot;;
      
             db.Customers.Add(customer);
             db.SaveChanges();
         }
      }
      

      }</pre>

      Every thing is saved fine but the CustomerId is changed from 3045 to 1 and saved.

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      Presumably what you actually mean is that when you save it and then look at the saved customer that the customer id is then 1 instead as you expected being 2945. Presuming that you are in fact looking at the correct database/table/column then I would suppose that the specific column is an auto index column. And you shouldn't be attempting to set the id in the first place.

      1 Reply Last reply
      0
      • C columbos14927

        Hello, I am a new in EF. I have a Customewr class:

        public class Customer
        {
        public int CustomerId { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        }

        And here is my DbContext class:

        public class DiveShopContext:DbContext
        {
        public DbSet<Customer> Customers { get; set; }
        }

        I want to be able to save Customers to my DB, i do it as follow:
        <pre lang="cs">class Program
        {
        static void Main(string[] args)
        {
        using (var db = new DiveShopContext())
        {

               var customer = new Customer();
               customer.CustomerId = 3045;
               customer.Name = &quot;Alex&quot;;
        
               db.Customers.Add(customer);
               db.SaveChanges();
           }
        }
        

        }</pre>

        Every thing is saved fine but the CustomerId is changed from 3045 to 1 and saved.

        S Offline
        S Offline
        S Douglas
        wrote on last edited by
        #3

        columbos14927 wrote:

        but the CustomerId is changed from 3045 to 1 and save

        I am going to guess that CustomerID at the database level is an Identity Column? If that's the case then the value you are trying to set is not going to be accepted by the database engine.


        Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups