Click here to Skip to main content
15,908,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i'm beginer to Entity Framework
Data Entity ModelConfiguration ModelValidationException when insert data odatabase
what is the wrong in my code?
Thank in advance!

This is my Object Class
C#
[Table ("customerDetails")]
    class customerDetails
    {
        [Key]
        internal int cusId { get; set; }
        internal string cusName { get; set; }
        internal string cusNic { get; set; }
        internal string cusAdd { get; set; }
        internal DateTime cusDob { get; set; }
    }.


This is DbContext
C#
class cusSave : DbContext
    {
        public cusSave() : base(@"Server= servername"
                            + "; Database= dbname"
                            + "; UID= user"
                            + "; Pwd= pass") { }
        public DbSet<customerDetails> saveCus { get; set; }
    }


This is the btn Event
C#
private void btnSave_Click(object sender, EventArgs e)
        {
            var cus =new Logic.customerDetails
            {
                cusId = Convert.ToInt32(txtCusId.Text),
                cusName = txtcusName.Text,
                cusAdd = txtcusAdd.Text,
                cusNic = txtcusNic.Text,
                cusDob = dtpDob.Value
            };
                var saveCus = new Logic.cusSave();
                saveCus.saveCus.Add(cus);
                saveCus.SaveChanges();
        }



Solution here
Change object class like this
Thanks Every One
Specially Graeme_Grant

C#
class customerDetails
    {
        [Key]
        public int cusId { get; set; }
        public string cusName { get; set; }
        public string cusNic { get; set; }
        public string cusAdd { get; set; }
        public DateTime cusDob { get; set; }
    }


What I have tried:

check all object values before save when assign to object but nothings wrong
Posted
Updated 21-Feb-17 4:19am
v3
Comments
Graeme_Grant 21-Feb-17 9:04am    
you have filled out the default fields (Server= servername, Database= dbname, UID= user, Pwd= pass) to connect to the database right?
Manoj Chamikara 21-Feb-17 9:07am    
No No actual values are different for the post i change values but same arguments
Graeme_Grant 21-Feb-17 9:17am    
Had to ask... :)
Graeme_Grant 21-Feb-17 9:16am    
Check the inner exception to see if there is additional information.
Manoj Chamikara 21-Feb-17 9:46am    
inner exception is null
this is all i get

One or more validation errors were detected during model generation:
EntityType 'customerDetails' has no key defined. Define the key for this EntityType.
saveCus: EntityType: EntitySet 'saveCus' is based on type 'customerDetails' that has no keys defined.

1 solution

Okay. The scope of the class &/or properties appears to be your problem. Change both to the class and the properties public and try again.
 
Share this answer
 
Comments
Manoj Chamikara 21-Feb-17 10:15am    
Thank You Very Much
That is the problem
i change the object class like you said then it's work fine
(Question updated)
Graeme_Grant 21-Feb-17 10:45am    
You are most welcome! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900