我首先使用EF代码开发我的3层WinForm应用程序,我使用了断开连接的POCO
s作为我的模型实体.我的所有实体都是从BaseEntity
类继承的.
我使用了断开连接的POCO
s,所以我State
在客户端处理实体,并且在ApplyChanges()
方法中,我将我的实体图(例如An Order
with it's OrderLines
and Products
)附加到我DbContext
,然后将每个实体State
与它的客户端同步State
.
public class BaseEntity
{
int _dataBaseId = -1;
public virtual int DataBaseId // DataBaseId override in each entity to return it's key
{
get { return _dataBaseId; }
}
public States State { get; set; }
public enum States
{
Unchanged,
Added,
Modified,
Deleted
}
}
Run Code Online (Sandbox Code Playgroud)
所以,当我想保存相关实体的图形时,我使用了以下方法:
public static EntityState ConvertState(BaseEntity.States state)
{
switch (state) …
Run Code Online (Sandbox Code Playgroud) 我与两个实体有一对多关系:
Order:
int OrderId
string OrderNumber
...
OrderItem:
int ItemId
int sequence
...
Product:
int ProductId
string ProductName
ProductType:
int ProductTypeid
string Title
Run Code Online (Sandbox Code Playgroud)
一个Order
有多个OrderItems
,每个OrderItem
有一个Product
,每个Product
有一个ProductType
。
我想编写返回所有订单他们一个LINQ items
,Product
,ProductType
和物品通过序列字段进行排序。如何编写查询,例如以下查询?
var myOrder= db.Orders.Include("OrderItems")
.Where(x => x.OrderId == 3)
.Include("OrderItems.Product")
.Include("OrderItems.Product.ProductType")
.Select(o => new {
order = o,
orderItems = o.OrderItems.OrderBy(i => i.sequence)
}).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
但是当它返回结果时,它不包含Product和ProductType数据。我的错误在哪里?
我想使用连接到excel 2007
文件(.xlsx)delphi 7
,所以我使用了a AdoConnection
并将其connectionstring
属性设置为:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test\qm\Results-summary.xlsx;Extended Properties="Excel 12.0;IMEX=1";Persist Security Info=False
Run Code Online (Sandbox Code Playgroud)
但是当我激活时,AdoConnection
我得到了这个错误:
找不到可安装的ISAM.
问题出在哪儿?
最近,我们的用户有时会遇到timeout expire 错误,当他们想要更新 DB 上的某些记录时。(我认为 DB 中发生了死锁之类的事情)我们的数据库是SQL Server 2008 R2
,我们的应用程序首先由 EF5 代码开发。
今天我读了一篇关于READ_COMMITTED_SNAPSHOT
在 SQL Server 中使用选项的文章,我认为这个选项可以帮助我们防止数据库中的死锁。根据那篇文章,它有两个步骤:
1-READ_COMMITTED_SNAPSHOT
在数据库中激活
ALTER DATABASE testDatabase SET ALLOW_SNAPSHOT_ISOLATION ON;
ALTER DATABASE testDatabase SET READ_COMMITTED_SNAPSHOT ON;
Run Code Online (Sandbox Code Playgroud)
2-READ_COMMITTED_SNAPSHOT
在代码中使用选项:
using (var transactionScope =
new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel= IsolationLevel.Snapshot }))
{
// update some tables using entity framework
context.SaveChanges();
transactionScope.Complete();
}
Run Code Online (Sandbox Code Playgroud)
这个示例,使用了TransactionScop
语句。但我们没有TransactionScop
用于管理Transactions
. 例如,我们的模型中有继承,当我们调用时.SaveChange()
,EF
创建和管理Transaction
它自己。
有READ_COMMITTED_SNAPSHOT
没有不用 …
c# entity-framework transactions sql-server-2008 database-deadlocks
的DbContext在EF Code first
器具的工作单位和存储库模式为
MSDN网站上说:
DbContext实例表示工作单元和存储库模式的组合,以便它可以用于从数据库进行查询并将更改组合在一起,然后将更改作为一个单元写回到存储中.DbContext在概念上类似于ObjectContext.
这是否意味着使用另一个UoW和Repository抽象(例如IRepository和IUnitOfWor),超过DbContext是错误的?
换句话说,在DbContext上使用另一个抽象层是否会为我们的代码添加任何其他值?
技术独立DAL(我们的域将取决于IRepository和IUnitofWork而不是DbContext)等值
entity-framework data-access-layer repository unit-of-work dbcontext
我遇到这种类型的错误,当我调试一个项目,我已经有了EntityFramework.dll
,System.Core.dll
,System.Data.Entity
但仍然Core
没有被引用。
有人能帮我吗?
我在这条线上有错误
using System.Data.Entity.Core.Objects;
Run Code Online (Sandbox Code Playgroud)
这是错误:
类型或名称空间名称“ Core”在名称空间“ System.Data.Entity”中不存在(您是否缺少程序集引用?)
我陷入了一个问题,需要投入。这是描述——
我有一个txtPenaltyDays
Windows 窗体 C#
private void txtPenaltyDays_TextChanged(object sender, EventArgs e)
{
if(Convert.ToInt16(txtPenaltyDays.Text) > 5)
{
MessageBox.Show("The maximum amount in text box cant be more than 5");
txtPenaltyDays.Text = 0;// Re- triggers the TextChanged
}
}
Run Code Online (Sandbox Code Playgroud)
但是我遇到了问题,因为这会触发 2 次。因为将文本值设置为 0。我的要求是它应该只触发一次并将值设置为 0。
任何建议都深表感谢。
Person
我的数据库中有一个NationalId
包含字段的表。有没有什么方法可以使用 Even NationalId
、 usingEf code first
和加载所有 Person Linq to entities
,而不将所有Person
s 加载到内存中?
像这样的东西:
public bool IsEven(int number)
{
return number % 2 == 0;
}
var context = new MyContext();
var personsWithEvenNationalId = context.Persons
.Where(x=> IsEven(x.NationalId))
.ToList();
Run Code Online (Sandbox Code Playgroud) 我想CommonServiceLocator.MefAdapter
在我的Visual Studio Community 2015项目中加入.当我尝试安装软件包时:https://www.nuget.org/packages/CommonServiceLocator.MefAdapter/1.0.0
我收到错误:
PM> Install-Package CommonServiceLocator.MefAdapter Install-Package:无法找到包'CommonServiceLocator.MefAdapter'在行:1 char:17 + Install-Package <<<< CommonServiceLocator.MefAdapter + CategoryInfo:NotSpecified:(:) [Install-包],Exception + FullyQualifiedErrorId:NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
c# ×7
dbcontext ×2
delphi ×1
delphi-7 ×1
dependencies ×1
excel-2007 ×1
include ×1
isam ×1
linq ×1
namespaces ×1
poco ×1
repository ×1
transactions ×1
unit-of-work ×1
winforms ×1