如何删除实体集合中的项目?
前任:
我有两个相互关联的实体
员工和参考
**Table: Employee**
EmployeeId(PK)
**Table: Reference**
ReferenceId(PK)
EmployeeId(FK)
Name
Run Code Online (Sandbox Code Playgroud)
首先我初始化这个:
Employee empCol = new Employee();
Reference refs = new Reference();
Run Code Online (Sandbox Code Playgroud)
然后我在实体集合中保存一些数据
refs.Name = "Sample";
empCol.References.Add(refs);
refs.Name = "Sample2";
empCol.References.Add(refs);
Run Code Online (Sandbox Code Playgroud)
我想删除集合中的第二个元素,我该怎么做?
[0]= {Name = "Sample"}
[1]= {Name = "Sample2"}
Run Code Online (Sandbox Code Playgroud)
我尝试了这种代码,但它不起作用,此代码不会删除我的实体集合中的第二个元素,但不会产生错误:
empCol.References.ToList().RemoveAt(1);
Run Code Online (Sandbox Code Playgroud) 我正在尝试在实体框架 4 中运行动态 SQL 查询
我将查询创建为字符串,它变成了这样:
SELECT
NodeID
,RowID
,[Comment]
,[Subject]
,[Text]
FROM
(SELECT
nr.Node_ID as NodeID
,nr.ID as RowID
,f.Name
,d.Value
FROM NoteNode.dbo.[NodeRows] AS nr
LEFT JOIN NoteNode.dbo.[NodeData] AS d ON nr.ID = d.NodeRow_ID
LEFT JOIN NoteNode.dbo.[NodeFields] AS f ON f.ID = d.Field_ID
WHERE nr.Node_ID = (1) ) AS x
PIVOT
(
MAX(Value)
for Name IN ([Comment],[Subject],[Text])
) AS p
Run Code Online (Sandbox Code Playgroud)
直接在 MSSQL Studio 中运行此代码完美无缺,完全没有问题.. 但是,当我尝试使用实体框架运行此代码时:
var DataRecords = new ObjectQuery<DbDataRecord>(dataQuery, (ObjectContext) _db.ObjectContext());
Run Code Online (Sandbox Code Playgroud)
它不起作用,并给我以下错误:
{“查询语法无效。接近标识符‘PIVOT’,第 17 行,第 31 列。”}
试图弄乱语法并清除所有换行符和制表符,但我一直回到这个错误。 …
我正在尝试在 LINQ/Entity Framework 中构造一个查询,该查询将返回基于一列具有重复项的所有行。但是,我不想对它们进行分组,我实际上想获取所有具有重复项的行。
name age
---- ---
john 15
john 16
jack 20
jill 26
sam 10
sam 12
Run Code Online (Sandbox Code Playgroud)
如何根据名称列获取具有重复项的行列表?这是我期待的结果集:
name age
---- ---
john 15
john 16
sam 10
sam 12
Run Code Online (Sandbox Code Playgroud)
我尝试了这样的事情,但我认为这会给我一个分组结果,我认为:
var duplicates = Shop.GroupBy(r => r.Name)
.Where(x => x.Count() > 1)
.Select(val => val.Key);
Run Code Online (Sandbox Code Playgroud) 我正在开发一个基于多租户的应用程序,该应用程序将为每个学校/租户提供一个单独的数据库。每个模式将彼此相同。这个想法是使用 .NET Core 3 和 EF Core 拥有一个数据库上下文。
例如,客户端导航到 school1.gov.uk,然后使用存储在 appsettings.json 中“school1”下的连接字符串实例化 SchoolContext。
目前,我必须针对添加的每个新上下文运行 add-migration。任何人都可以想出一个解决方案来在单个上下文(即 SchoolContext )下运行一次迁移吗?
主要上下文
public class SchoolContext : DbContext
{ protected readonly IConfiguration _configuration;
private readonly IHttpContextAccessor _httpContextAccessor;
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public SchoolContext(IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
{
_configuration = configuration;
_httpContextAccessor = httpContextAccessor;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var subdomain = _httpContextAccessor.HttpContext.Request.Host.Host;
var connectionString = _configuration.GetConnectionString(subdomain);
optionsBuilder.UseSqlServer(connectionString);
}
}
Run Code Online (Sandbox Code Playgroud)
租户上下文 1
public class …Run Code Online (Sandbox Code Playgroud) .net entity-framework entity-framework-4 .net-core asp.net-core
如何获取此类查询的跟踪字符串:
var product = _context.Products.Where( p => p.Category == "Windows" )
.SingleOrDefault();
// I can't do this since product is not an ObjectQuery instance
// product.ToTraceString();
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用EF版本4中的Dynamic where子句创建一个查询,并且唯一生成的where子句是初始语句中的"c.clientid == Clientid".查看SQL事件探查器时,result.where语句未附加到结果变量.你能告诉我我做错了什么吗?
var result = (from c in cxt.Customer
where c.clientid == Clientid
select c);
string employeenumber = formdata["employeenumber"].ToString();
if (!string.IsNullOrWhiteSpace(employeenumber))
{
result.Where(t => t.EmployeeNumber.ToLower() == employeenumber);
}
string FirstName = formdata["FirstName"].ToString();
if (!string.IsNullOrWhiteSpace(FirstName))
{
result.Where(t => t.FirstName.ToLower().Contains(FirstName));
}
return result.ToList();
Run Code Online (Sandbox Code Playgroud) 使用实体框架4.我以前创建了一个Code First数据库,一段代码需要删除并添加16个对象,每个需要6秒.每个查询的时间超过300毫秒!
删除/添加发生在foreach范围内,并且在SaveChanges()外部foreach.

在上面的图像中,您可以看到,对于16个呼叫,每个都需要6秒,这是34%的时间.
这对我来说听起来不正常...... 为什么会这样,我怎样才能提高性能呢?
如果没有解决方案:我可以使用任何变通方法吗?重写我的项目会很痛苦......
我有实体框架的问题.特别是我不能做插入或更新操作.错误信息:
An error occurred while updating the entries. See the inner exception for details.System.Data.SqlClient.SqlException (0x80131904): The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) …Run Code Online (Sandbox Code Playgroud) 所以,情况如下 -
那么任何人都可以随时遇到类似问题吗?我在这做错了什么?
我正在尝试初始化EFUnitOfWorkFactory对象上下文.
在global.asax中:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
ObjectFactory.Initialize(x =>
{
x.For<IUnitOfWorkFactory>().Use<EFUnitOfWorkFactory>();
x.For(typeof(IRepository<>)).Use(typeof(EFRepository<>));
}
);
EFUnitOfWorkFactory.SetObjectContext(() => new MyEntities());
}
Run Code Online (Sandbox Code Playgroud)
在生成的Object上下文中,我有:
public partial class MyEntities : ObjectContext
{
.....
.....
}
Run Code Online (Sandbox Code Playgroud)
我在执行Web应用程序时遇到异常EFUnitOfWorkFactory.SetObjectContext(() => new MyEntities());:
Compiler Error Message: CS0012: The type 'System.Data.Objects.ObjectContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Run Code Online (Sandbox Code Playgroud)
我将System.Data.Entity添加到我的Web应用程序两次并重建但仍然收到此消息.另外,gobal.asax中的这一行有时标有红色下划线.
有谁知道如何解决这个问题?
c# ×3
.net ×2
linq ×2
.net-core ×1
asp.net ×1
asp.net-core ×1
c#-4.0 ×1
code-first ×1
datacontext ×1
entity ×1
performance ×1
sync ×1