Click here to Skip to main content
15,887,343 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
Hello
i want saving record in database using entity framework but when save from table error "no key"
iam not recreation table and defined primary key on a table
please help me
regards
yarian

What I have tried:

Code Me In Model :
namespace Entity.EF
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("TblUser")]
public partial class User
{
public User()
{

}
~User()
{

}

public User(int Id,string A,string B,string C)
{
this.Id = Id;
this.A = A;
this.B = B;
this.C = C;
}
public int Id { get; set; }
[StringLength(50)]
public string A { get; set; }
[StringLength(50)]
public string B { get; set; }
[StringLength(50)]
public string C { get; set; }
}
}


Code Me In DAL :
namespace DataAL
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public class TurboTec :DbContext
{
public TurboTec():base("name=ConDb")
{
}
~TurboTec()
{

}
public System.Data.Entity.DbSet UserS { get; set; }
public System.Data.Entity.DbSet TableS { get; set; }
}
}


Code Me Windows Form :
Entity.EF.User UserObj = new Entity.EF.User();
TurboTec db = new TurboTec();
UserObj.A = "A";
UserObj.B = "B";
UserObj.C = "C";
db.UserS.Add(UserObj);

DataBase SqlServer(No Primary Key And Fk):
Name Table User
Field 1 : [A] nvarchar(50)
Field 2 : [B] nvarchar(50)
Field 3 : [C] nvarchar(50)
Posted
Updated 10-Aug-16 21:22pm

Hi..As primary key is not defined in the table issue is occurring..Kindly declare a primary key on table TblUser and then update the model.
 
Share this answer
 
Comments
misaqyrn9677 10-Aug-16 2:50am    
I Not Working Or Use Primary Key In Table But Save Record
 
Share this answer
 
C#
You can't. An entity MUST have a unique, immutable ID. It doesn't have to be defined as a primary key in the database, but the field or set of fields must uniquely identify the row, and its value may not change.

So, if one field in your entity, or one set of fields in your entity, satisfies these criteria, make it (or them) the ID of the entity. For example, if there is no way that a user can create two instances in the same day, you could make [A, B] the ID of the entity.

Of course this is a bad solution, and you should really change your schema and add an autogenerated, single-column ID in the entity
.
 
Share this answer
 
Comments
misaqyrn9677 10-Aug-16 23:35pm    
I Not Working Or Use Primary Key In Table But Save Record
The Praveen Singh 11-Aug-16 0:29am    
please explain what are you trying to ask..
misaqyrn9677 11-Aug-16 0:42am    
Iam Working In OTC That Before OneBody Design DataBase And Used DataBase And All Table Not Have Primary Key
My Employer Say Don't Use My Table Should don't Use Primary Key
The Praveen Singh 11-Aug-16 2:35am    
if can't use primary key then don't use EF because its necessary to be a primary key in table used in EF.
Could you please provide table schema here...
 
Share this answer
 
Comments
misaqyrn9677 10-Aug-16 2:16am    
Create Table [dbo].[TblUser](
[A] nvarchar(50) NULL,
[B] nvarchar(50) NULL,
[C] nvarchar(50) NULL
);

Without Fkey
I Solving The Error When Use EF You Must Have Primary key
But When You Should Use PK in DataBase No Have Problem Then You'r Model One Field Set 'KEY' And Then Run It Not Error

Create Table [dbo].[TblUser](
[A] nvarchar(50) NULL,
[B] nvarchar(50) NULL,
[C] nvarchar(50) NULL
);


C#
[key]
<pre lang="C#">public string A { get; set; }</pre>
 
Share this answer
 

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