我正在尝试使用Dapper.Contrib更新此表:
public class MyTable
{
public int ID { get; set; }
public int SomeColumn1 { get; set; }
public int SomeColumn2 { get; set; }
public int CreateUserID { get; set; }
public int UpdateUserID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我不想更新CreateUserID列,因为它是一个更新方法,所以我想在调用Dapper - Update.Async(entity)方法时忽略此列.
我尝试使用[NotMapped]和[UpdateIgnore]属性,但没有帮助.
注意:我仍然希望在插入操作上传递此列,因此,[Computed]和[Write(false)]不合适.
有人可以帮我弄清楚在更新数据库中的表时如何忽略此列?
提前致谢.
我正在尝试使用Dapper.Contrib在主键列不是标识列的表中插入数据.
使用此脚本创建数据库表:
begin transaction
create table
dbo.Foos
(
Id int not null,
Name nvarchar(max) not null
)
go
alter table
dbo.Foos
add constraint
PK_Foos primary key clustered
(
Id
)
go
commit
Run Code Online (Sandbox Code Playgroud)
这是C#类:
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
插入这样的数据时:
connection.Insert(new Foo()
{
Id = 1,
Name = "name 1"
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
无法将值NULL插入列'Id',表'FooDatabase.dbo.Foos'; 列不允许空值.INSERT失败.
按照惯例,Dapper正确地假设它Id是主键,但它错误地假设它是一个标识列.我如何表明它不是标识列?
我正在使用Dapper和Dapper.Contrib在MVC5 c#环境中,有时(!)当我部署生产站点时,我得到错误说明:
GetAll<T>在Dapper.Contrib.Extensions.SqlMapperExtensions.GetAllAsync [T](IDbConnection连接,IDbTransaction)的Dapper.Contrib.Extensions.SqlMapperExtensions.GetSingleKey [T](String方法)中仅支持具有单个[Key]或[ExplicitKey]属性的实体transaction,Nullable`1 commandTimeout)
这只发生在每三个部署一次.
我怀疑它会以某种方式Dapper.Contrib自动注意到我的主键,因为它的名字是"Id",但我装饰它[ExplicitKey](它是一个GUID),也许这些属性会发生冲突.也许这是完全不同的东西......
关于如何解决这个问题的任何想法,除了可能重命名我的主键?
有问题的模型中的一块:
[Table("Tasks")]
public class TasksModel
{
[ExplicitKey]
public Guid Id { get; set; }
...
Run Code Online (Sandbox Code Playgroud) 我有一个对象,其属性名称恰好命名DB表中的字段名称,但我不知道如何插入它.唯一不同的是DB表名.所以它是一个具有不同模型/映射表名称的对象,但我希望将它插入到一个名称与模型不同的表中.我试过这个:
var val = info.FooBarObj;
conn.Execute("insert DBInformation(val) values(@val)", new { val = val });
Run Code Online (Sandbox Code Playgroud)
例如
对象是FooBarObj和属性int Id, string Foo, string Bar
并且DBInformation具有字段名称:Id, Foo, and Bar但是没有调用该表,而是调用FooBarObj它DBInformation.
我该如何插入这样的东西?我正在使用Dapper
编辑:
我可以为FooBar模型提供两个表属性吗?
例如[Table("DBInformation")]和[Table("FooBar")].
我有一个奇怪的边缘情况,如果发生这种情况我想插入FooBar,如果发生另一种情况,插入DBInformation.这就是我目前面临的问题,因此我不能只为这个问题添加属性并完成.
我有一个 .Net Core 3.1 控制台应用程序。
在SQL Server数据库中,我的表是单数的,和我的POCO类一样,方便匹配和维护。
对于插入、更新和删除操作,我想使用Dapper.Contrib库。但是当我在生成的 SQL 查询中运行插入函数 Dapper 时,将表名复数化。
SQL 表“学生”:
CREATE TABLE Student
StudentId int NOT NULL PRIMARY KEY,
FirstName varchar(50),
LastName varchar(50)
Run Code Online (Sandbox Code Playgroud)
C# 代码
public class Student
{
public int StudentId {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
class Program
{
static void Main(string[] args)
{
var students = new List<Student>()
{
new Student { StudentId = 1, FirstName = "John", LastName = "Doe"},
new Student { …Run Code Online (Sandbox Code Playgroud) 此资源解释了如何Computed排除属性(仅在更新中?)。
指定应从更新中排除该属性。
Run Code Online (Sandbox Code Playgroud)[Table("Invoice")] public class InvoiceContrib { [Key] public int InvoiceID { get; set; } public string Code { get; set; } public InvoiceKind Kind { get; set; } [Write(false)] [Computed] public string FakeProperty { get; set; } } using (var connection = My.ConnectionFactory()) { connection.Open(); var invoices = connection.GetAll<InvoiceContrib>().ToList(); // The FakeProperty is skipped invoices.ForEach(x => x.FakeProperty += "z"); var isSuccess = connection.Update(invoices); }
不Write(false)达到同样的目的吗?[Computed]和 和有[Write(false)]什么区别?
编辑:
我刚刚检查了为回答我的问题而链接的资源。它几乎一针见血!有人可以确认这两个属性是否执行相同的操作,但只是以两种不同的方式表达,以便为用户提供更好的抽象?
我一直很好奇Dapper(或许其他ORM)在与LINQ结合时如何处理对象检索.
如果我有这样的课程:
public static IEnumerable<SitePage> GetAll()
{
using (IDbConnection cn = new SqlConnection(g.Global.CONX))
{
cn.Open();
return cn.GetAll<SitePage>();
}
}
Run Code Online (Sandbox Code Playgroud)
我构建一个这样的查询:
var result = SitePage.GetAll().Select(c=> new { c.id, c.PageUrl, c.ParentId });
Run Code Online (Sandbox Code Playgroud)
我很好奇,如果在后台,整个记录集被拉入包括所有其他列(可能包含非常大的varchars),或者Dapper是否从这个查询中理解只是为了从sql db中提取我请求的列?我意识到它有点像新手,但我想更好地理解Dapper/LINQ交互.
这里发布了一个类似的问题:选择特定列 - 使用linq-what-gets-transferred,但我不确定是否已经完全回答.海报有2个问题,也没有使用我通常喜欢的lambda表达式.
对此的回答将使我的思绪发火(并且很可能改变我编码的方式,因为我一直谨慎并且觉得我通过显式sql编写了太多代码).
我在这门课上遇到了以下挑战:
Public Class MyClass
Property Id As Integer
Property LastName as String
End Class
Run Code Online (Sandbox Code Playgroud)
数据库中对应的数据表有如下字段:
Id (int, not null)
Last-Name (nvarchar(80),null)
所以我需要映射MyClass.LastName到MyClasses.Last-Name并度过一段地狱般的时光......
当我编写自定义插入查询时,一切正常,但我想使用 Dapper 扩展包之一的 .Insert 语句。
我尝试过 Dapper.Contrib,但这会忽略我使用 Dapper.FluentMap 或使用 Dapper 本身的内置方法创建的映射Dapper.SetTypeMap。
我尝试了 Dapper.FastCrud,但我无法弄清楚如何为其配置映射,尽管这个 API 看起来很有前途。
任何人?
我的班级如下:
[Table("tblUser")]
public class User
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用Dapper.Contrib,有没有办法通过而不是Id来获取User记录?Title
如果我像下面这样查询,它就有效。但是,我想过滤Title不是键的列。
await connection.GetAsync<User>(Id);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Dapper.ContribIDbConnection以及其他.insert()方法来扩展接口的功能。
为此,我在此处和此处遵循了一些简短而分散的文档。总之,我使用NuGet添加Dapper和Dapper.Contrib我的项目,我已经加入using Dapper;,并using Dapper.Contrib;在我的仓库类的顶部,和我使用的System.Data.SqlClient.SqlConnection()创建IDbConnection。
仍然,我的连接对象没有可用的扩展方法。例如,当尝试使用该.insert()方法时,我收到消息:
'IDbConnection'C#不包含'Insert'的定义,找不到找不到接受第一个类型参数的扩展方法'Insert'(您是否缺少using指令或程序集引用?)
这是在使用Razor Pages的ASP.NET Core 2.0项目中。
为了完整起见,您可以在下面找到Repository类。
也许有趣的是,usingDapper和Dapper.Contrib 的行显示为灰色...
另外,当然,我有一个TEST实体(非常简单)的Model类,其中包含一个参数TEST_COLUMN,用注释[Key]。
using Dapper.Contrib;
using Dapper;
using Microsoft.Extensions.Configuration;
using TestProject.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace TestProject.Repository
{
public class TEST_Repository
{
IConfiguration configuration;
public TEST_Repository(IConfiguration configuration)
{
this.configuration = …Run Code Online (Sandbox Code Playgroud) dapper ×10
dapper-contrib ×10
c# ×6
sql-server ×3
.net-core ×1
asp.net ×1
asp.net-core ×1
attributes ×1
linq ×1
razor ×1