我目前正在开发MVC 3中的项目.我已将我的问题分开,因此有一些项目,如Core,Repository,UI,Services等.我实现了Repository,UnitOfWork,最重要的是State模式.
我正在使用Entity Framework 4.3来保存我的数据,并且我遇到了一个涉及当前状态持久性的相当烦人的情况.以下是一些类示例:
public class Request
{
public int RequestId { get; set; }
public State CurrentState { get; set; }
}
public abstract class State
{
[Key]
public string Name {get; set;}
public virtual void OpenRequest(Request request)
{}
public virtual void CloseRequest(Request request)
{}
}
public class RequestIsOpenState : State
{
public RequestIsOpenState()
{
this.Name = "Open";
}
public override void CloseRequest(Request request)
{
request.CurrentState = new RequstIsClosedState();
}
}
public class RequestIsClosedState : State
{
public …Run Code Online (Sandbox Code Playgroud) 我收到以下错误
"无法加载在名称为'MySql.Data.MySqlClient'的ADO.NET提供程序的应用程序配置文件中注册的实体框架提供程序类型'MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity'.确保使用了程序集限定名称,程序集可供正在运行的应用程序使用.有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882."
但是我的项目中引用了MySql.Data.dll和MySql.Data.Entity.dll以及MySql.Data.Entity.EF6.dll(来自MySQL Connector Net 6.8.3)
这是我的App.conf
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="inspectm_inspectContext" connectionString="server=--user id=--;password=--;database=--;persistsecurityinfo=True" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity" />
</providers>
</entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud) 我当前正在实现存储库模式,该模式允许我将类和上下文传递到常规存储库中。这很正常。我感兴趣的方法是Get and Find。该发现使我可以指定和“订购”一些“ where”语句和一些“ include”语句。这是理想的。
public class GeneralRepository<TEntity> : IGeneralRepository<TEntity> where TEntity : class
{
readonly GamesContext context;
readonly DbSet<TEntity> db;
public GeneralRepository(GamesContext existingContext)
{
context = existingContext;
db = context.Set<TEntity>();
}
public TEntity Get(object id)
{
return db.Find(id);
}
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = db;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, …Run Code Online (Sandbox Code Playgroud) 使用实体框架的API,我不断遇到以下两种方式来映射多对多关系?我从未使用过第二个选项...有什么区别?
选项1:
modelBuilder.Entity<Student>()
.HasMany( p => p.Lessons)
.WithMany();
Run Code Online (Sandbox Code Playgroud)
选项2:
modelBuilder.Entity<Student>()
.HasMany(p => p.Lessons)
.WithMany()
.Map(m =>
{
m.MapLeftKey("Id");
m.MapRightKey("Id");
m.ToTable("StudentAndLessons");
});
Run Code Online (Sandbox Code Playgroud)
这到底是什么MapLeftKey和MapRightKey做什么?您何时使用它并获得什么好处?