实体框架中的NEWID()SQL Server函数 [英] NEWID() SQL Server Function in Entity Framework

查看:96
本文介绍了实体框架中的NEWID()SQL Server函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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屋!

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