想要使用实体框架在数据库中保存记录,但是当从表中保存错误“无密钥”时 [英] Want saving record in database using entity framework but when save from table error "no key"

查看:46
本文介绍了想要使用实体框架在数据库中保存记录,但是当从表中保存错误“无密钥”时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i希望使用实体框架在数据库中保存记录但是当从表中保存错误无密钥时

iam不是重新创建表并在表上定义主键

请帮帮我

问候

yarian



我有什么试过:



Code Me In Model:

namespace Entity.EF

{

使用System;

使用System.Collections.Generic;

使用System.ComponentModel.DataAnnotations;

使用System。 ComponentModel.DataAnnotations.Schema;

使用System.Data.Entity.Spatial;

[表(TblUser)]

公共部分类用户

{

公众用户()

{



}

~用户()

{



}



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;组; }

[StringLength(50)]

public string A {get;组; }

[StringLength(50)]

public string B {get;组; }

[StringLength(50)]

public string C {get;组; }

}

}





代码我在DAL:

命名空间DataAL

{

使用System;

使用System.Data.Entity;

使用System.ComponentModel.DataAnnotations.Schema;

使用System.Linq;

公共类TurboTec:DbContext

{

public TurboTec():base(name = ConDb)

{

}

~TurboTec()

{



}

public System.Data.Entity.DbSet UserS {get;组; } $ / $
public System.Data.Entity.DbSet TableS {get;组; }

}

}





Code Me Windows窗体:

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(无主键和Fk):

名称表用户

字段1 :[A] nvarchar(50)

字段2:[B] nvarchar(50)

字段3:[C] nvarchar(50)

解决方案

嗨......由于表中没有定义主键问题正在发生。请在表 TblUser 上声明主键,然后更新模型。


嘿..请参考下面的链接



有抱负的架构师:实体框架:添加没有实体模型主键的DataTable。 [ ^ ]



这可以帮到你..


你可以' < span class =code-string> t。实体必须具有唯一的不可变ID。它不必在中定义为数据库中的主键,但是字段或 set 字段必须唯一标识该行,并且其可能不会更改。 

所以,如果一个字段 你的实体,或一个>设置字段 在您的实体中,满足这些条件,使其(或它们)成为实体的ID。例如,如果那么 用户无法创建两个实例当天,您可以将[A,B]作为实体的ID。

当然 是一个糟糕的解决方案,你应该真的改变你的schema和实体

中添加自动生成的单列ID


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)

解决方案

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.


Hey..pls refer below link

Aspiring Architect: Entity Framework: Adding DataTable with no Primary Key to Entity Model.[^]

This may help you here..


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

.


这篇关于想要使用实体框架在数据库中保存记录,但是当从表中保存错误“无密钥”时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆