我有以下课程DbContext
:
public class Order:BaseEntity
{
public Number {get; set;}
}
Product:BaseEntity;
{
public Name {get; set;}
}
public class Context : DbContext
{
....
public DbSet<Order> Orders { set; get; }
public DbSet<Product> Products { set; get; }
....
}
Run Code Online (Sandbox Code Playgroud)
我有一个想要添加到我的上下文的对象列表,但我不知道如何DbSet
根据每个实体类型动态地找到合适的泛型.
IList<BaseEntity> list = new List<BaseEntity>();
Order o1 = new Order();
o1.Numner = "Ord1";
list.Add(o1);
Product p1 = new Product();
p1.Name = "Pencil";
list.Add(p1);
Context cntx = new Context();
foreach (BaseEntity entity in list)
{
cntx.Set<?>().Add(entity); …
Run Code Online (Sandbox Code Playgroud) 在使用EF更新期间出现以下错误:
操作失败:无法更改关系,因为一个或多个外键属性不可为空.当对关系进行更改时,相关的外键属性将设置为空值.如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象.
有没有一般的方法来查找哪些外键属性导致上述错误?
[更新]
对于以下代码导致上述错误的一种情况(我在断开连接的环境中工作,所以我曾经graphdiff
更新过我的对象图),当它想要运行时_uow.Commit();
:
public void CopyTechnicalInfos(int sourceOrderItemId, List<int> targetOrderItemIds)
{
_uow = new MyDbContext();
var sourceOrderItem = _uow.OrderItems
.Include(x => x.NominalBoms)
.Include("NominalRoutings.NominalSizeTests")
.AsNoTracking()
.FirstOrDefault(x => x.Id == sourceOrderItemId);
var criteria = PredicateBuilder.False<OrderItem>();
foreach (var targetOrderItemId in orderItemIds)
{
int id = targetOrderItemId;
criteria = criteria.OR(x => x.Id == id);
}
var targetOrderItems = _uow.OrderItems
.AsNoTracking()
.AsExpandable()
.Where(criteria)
.ToList();
foreach (var targetOrderItem in targetOrderItems)
{
//delete old datas and insert new datas
targetOrderItem.NominalBoms …
Run Code Online (Sandbox Code Playgroud) 我先使用EF代码.此外,我正在为我的所有存储库使用基本存储库,并IUnitofWork
注入存储库:
public interface IUnitOfWork : IDisposable
{
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
int SaveChanges();
}
public class BaseRepository<T> where T : class
{
protected readonly DbContext _dbContext;
protected readonly IDbSet<T> _dbSet;
public BaseRepository(IUnitOfWork uow)
{
_dbContext = (DbContext)uow;
_dbSet = uow.Set<T>();
}
//other methods
}
Run Code Online (Sandbox Code Playgroud)
例如,我OrderRepository
是这样的:
class OrderRepository: BaseRepository<Order>
{
IUnitOfWork _uow;
IDbSet<Order> _order;
public OrderRepository(IUnitOfWork uow)
: base(uow)
{
_uow = uow;
_order = _uow.Set<Order>();
}
//other methods
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式使用它: …
我使用领域驱动n层应用程序体系结构与EF code first
我在最近的项目中,我定义我的Repository
合同,在Domain
层.制定其他Repositories
不那么冗长的基本合同:
public interface IRepository<TEntity, in TKey> where TEntity : class
{
TEntity GetById(TKey id);
void Create(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
}
Run Code Online (Sandbox Code Playgroud)
并且Repositories
每个都是专门的Aggregation root
,例如:
public interface IOrderRepository : IRepository<Order, int>
{
IEnumerable<Order> FindAllOrders();
IEnumerable<Order> Find(string text);
//other methods that return Order aggregation root
}
Run Code Online (Sandbox Code Playgroud)
如您所见,所有这些方法都依赖于Domain entities
.但在某些情况下,应用程序UI
需要一些不是Entity
数据的数据,这些数据可能来自两个或更多的肠炎数据View-Model
,在这些情况下,我定义了View-Model
s in Application layer
,因为它们非常依赖于Application's
需求而不是到了 …
假设我有类:
public class Order
{
int OrderId {get; set;}
string CustomerName {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我也在下面声明了变量
Func<Order, bool> predicate1 = t=>t.OrderId == 5 ;
Func<Order, bool> predicate2 = t=>t.CustomerName == "Ali";
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以连接这些变量(使用AND/OR)并将结果放在第三个变量中?例如:
Func<Order, bool> predicate3 = predicate1 and predicate2;
Run Code Online (Sandbox Code Playgroud)
要么
Func<Order, bool> predicate3 = predicate1 or predicate2;
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚为什么即使在重新启动计算机后仍在使用端口!
System.ServiceModel.AddressAlreadyInUseException:IP端点0.0.0.0:13000上已有一个侦听器.如果有另一个应用程序已在此端点上侦听,或者如果服务主机中有多个服务端点具有相同的IP端点但具有不兼容的绑定配置,则可能会发生这种情况.---> System.Net.Sockets.SocketException:System的System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)通常只允许使用每个套接字地址(协议/网络地址/端口). Net.Sockets.Socket.Bind(端点localEP)在System.ServiceModel.Channels.SocketConnectionListener.Listen()---内部异常堆栈跟踪的末尾在System.ServiceModel.Channels.SocketConnectionListener.Listen()在系统. ServiceModel.Channels.TracingConnectionListener.Listen()在System.ServiceModel.Channels.ConnectionAcceptor.StartAccepting()在System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()在System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)的系统. ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)在System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(时间跨度超时)在System.ServiceModel.Channels.Communicat ionObject.Open在System.ServiceModel.Channels.CommunicationObject.Open(时间跨度超时)在System.ServiceModel.ServiceHostBase.OnOpen(时间跨度超时)在系统(时间跨度超时)在System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(时间跨度超时). ServiceModel.Channels.CommunicationObject.Open(时间跨度超时)在Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo信息)System.Net.Sockets.SocketException(0X80004005):每个套接字地址中的一个使用量(协议/网络地址/端口)在System.Net.Sockets.Socket.DoBind(端点endPointSnapshot,为SocketAddress的SocketAddress)在System.Net.Sockets.Socket.Bind(端点localEP)在System.ServiceModel.Channels.SocketConnectionListener.Listen正常允许()
你怎么知道哪个进程正在侦听该端口(13000)?Netstat在该端口上没有显示任何内容.
这是我的App.config:
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="SomeTarget.SomeTargetService">
<endpoint address="" binding="customBinding" bindingConfiguration="NetTcpBinding"
contract="SomeTarget.ISomeTargetService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:13000" />
</baseAddresses>
</host>
</service>
</services>
<bindings> …
Run Code Online (Sandbox Code Playgroud) 如何将IList<Customer>
列表转换为BindingList<Customer>
?
在C#表单中,我有一个面板固定在所有侧面,并在内部,一个文本框,锚定顶部/左/右.
当文本加载到文本框中时,我希望它自动垂直展开,这样我就不需要滚动文本框(如果有更多文本不适合面板,最多滚动面板).有没有办法用文本框这样做?(我不限制使用此控件,所以如果有另一个符合描述的控件,请随意提及)
我跟随两个实体,我试图使用外键关联(一对一).
public class StandardRack {
public int Id {get;set}
public StandardRelay StandardRelay {get;set}
}
public class StandardRelay {
public int Id {get;set}
public int StandardRack_Id {get;set;}
[Required][ForeignKey("StandardRack_Id")]
public StandardRack StandardRack { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这会抛出ModelValidationException.任何想法为什么这样一个看似简单的一对一双向关系无法配置.
编辑:
这是例外:
捕获到System.Data.Entity.ModelConfiguration.ModelValidationException消息=在模型生成期间检测到一个或多个验证错误:
System.Data.Edm.EdmAssociationEnd :: Multiplicity在关系'StandardRelay_StandardRack'中的角色'StandardRelay_StandardRack_Source'中无效.由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为*.
Source = EntityFramework StackTrace:位于System.Data.Entity的System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel model)的System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model,XmlWriter writer) .DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)在System.Data.Entity.DbModelBuilder.Build(的DbConnection providerConnection)在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)在System.Data.Entity.Internal. RetryLazy
2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet
1.Initialize()在System.Linq.Enumerable.ToList的System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery
1.System.Collections.Generic.IEnumerable.GetEnumerator()at System.Collections.Generic.List1..ctor(IEnumerable
1 collection) [TSource](IEnumerable`1 source)位于D:\ RailwayProjects\RelayAnalysis\TestApplication\MainWindow.xaml.cs中的TestApplication.MainWindow.Window_Loaded(Object …
我的项目中有一个接口,有2个类实现它:
public interface IService
{
int DoWork();
}
public class Service1:IService
{
public int DoWork()
{
return 1;
}
}
public class Service2:IService
{
public int DoWork()
{
return 2;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个依赖于它的命令处理程序IService
:
public CommandHandler1:ICommandHandler<CommandParameter1>
{
IService _service;
public CommandHandler1(IService service)
{
_service = service
}
public void Handle()
{
//do something
_service.DoWork();
//do something else
}
}
public interface ICommandHandler<TCommandParameter>
where TCommandParameter :ICommandParameter
{
void Handle(TCommandParameter parameter);
}
public interface ICommandParameter
{
}
Run Code Online (Sandbox Code Playgroud)
我想基于用户选择注入Service1
或Service2 …