小编Heb*_*rda的帖子

使用Entity Framework保持状态模式

我目前正在开发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)

c# state entity-framework unit-of-work ef-code-first

15
推荐指数
2
解决办法
1753
查看次数

使用MySQL的Enity框架

我收到以下错误

"无法加载在名称为'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)

mysql entity-framework

11
推荐指数
2
解决办法
3万
查看次数

默认情况下,渴望通过通用存储库中的实体框架加载所有内容

我当前正在实现存储库模式,该模式允许我将类和上下文传递到常规存储库中。这很正常。我感兴趣的方法是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)

c# linq asp.net-mvc entity-framework repository-pattern

5
推荐指数
1
解决办法
5130
查看次数

在实体框架中使用Fluent API创建多对多关系

使用实体框架的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)

这到底是什么MapLeftKeyMapRightKey做什么?您何时使用它并获得什么好处?

c# entity-framework fluent-interface

2
推荐指数
1
解决办法
1267
查看次数