实体框架中的NEWID()SQL Server函数 [英] NEWID() SQL Server Function in Entity Framework
问题描述
我正在使用 AngularJS , ASP.NET ,实体框架和 SQL Server 开发Web应用程序数据库.
I am developing a web application using AngularJS, ASP.NET, Entity Framework and a SQL Server database.
我想使用 EF 通过这种方法插入一些行:
I want to insert some rows using EF with this method :
public string addRow(int CaptionID, string enLang)
{
string output = "";
try
{
var Cap = new Table_Name();
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Cap.CodeId = CaptionID;
Cap.Created = DateTime.Now;
Cap.CreatedBy = userName;
Cap.ID = "";
Cap.CodeLanguage = "en";
Cap.CodeCountry = "GB";
Cap.Caption = enLang;
_db.Entry(Cap).State = EntityState.Added;
_db.SaveChanges();
output = "Created";
}
catch (Exception ex)
{
output = ex.InnerException.Message;
}
return output;
}
此功能有效.
但是我对 NEWID()
函数有疑问,我进行了很多搜索,以了解如何重现相同的内容,但我不知道,我已经看过了这个
But I have a question about the NEWID()
function, I have searched a lot to know how to reproduce the same but I didn't knew, I've looked this thread but nothing.
实际上,我想将 NEWID()
分配给 Cap.ID
.
In fact, I want to assign NEWID()
to Cap.ID
.
PS :我知道可以通过向 Cap.ID
分配 SELECT NEWID()
来使用SQLServer做到这一点,但是我正在寻找一种使用 EF 的方法.
P.S : I know that is it possible to do it using SQLServer by assigning an SELECT NEWID()
to Cap.ID
, but I am looking for a way to do it using EF.
推荐答案
只需执行: Cap.ID = Guid.NewGuid();
这篇关于实体框架中的NEWID()SQL Server函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!