标签: ef-code-first

如何在实体框架代码中有多对多关联

我刚刚开始使用EF,我观看了一些精彩的教程视频.我坚持以下.

我有一个文件集合的类,我希望这些与事件和/或人联系在一起

public class file{
    public int id {get;set;}
    public string path {get;set;}
}

public event {
    public int id {get;set;}
    public string eventname {get;set}
    public virtual ICollection<file> files {get;set;}
    public event(){ files = new list<file>();}
}

public person {
    public int id {get;set;}
    public string name {get;set}
    public virtual ICollection<file> files {get;set;}
    public person(){ files = new list<file>();}
}
Run Code Online (Sandbox Code Playgroud)

现在当我生成数据库时,我的文件表有一个PersonID AND EventID.

我希望能够让用户将文件附加到人和/或事件.

c# entity-framework entity-framework-4 ef-code-first

27
推荐指数
1
解决办法
1万
查看次数

使用代码EF CTP5生成货币类型字段

在这篇博客文章中:EF4 Code First Control Unicode和Decimal Precision,Scale with Attributes,Dane Morgridge使用属性来控制数据库中不同类型的创建.

......我发现这个非常独特的BTW!

如何使用EF CTP5的代码优先API在生成的数据库中生成货币类型字段,是否可以使用约定或属性从模型中执行此操作?

抱歉,我的英语不是我的主要语言.

提前致谢.

entity-framework ef-code-first entity-framework-ctp5

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

实体框架CodeFirst与其他信息的多对多关系

我有以下型号:

class Contract
{
   string ContractID{get;set;}
   ICollection<Part> Parts{get;set;}
}

class Part
{
   string PartID{get;set;}
   ICollection<Contract> Contracts{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

问题是Part和Contract之间的关系还包含以下附加信息:

class ContractParts
{ 
   Contract{get;set;}
   Part{get;set;}
   Date{get;set;} //additional info
   Price{get;set;} //additional info
}
Run Code Online (Sandbox Code Playgroud)

我将如何为此编写实体上下文?

.net c# entity-framework ef-code-first entity-framework-4.1

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

在WPF中是否可以使用RecreateDatabaseIfModelChanges?

我正在尝试实体框架代码优先.我似乎无法RecreateDatabaseIfModelChanges在WPF 4.0中找到要使用的程序集/命名空间.这是仅限ASP.NET的功能吗?如果没有,我应该参考哪个装配?

这是我的代码:

using System;
using System.Data.Entity;
using System.Windows;
using CodeFirstTester.Models;

namespace CodeFirstTester
{
    public partial class App : Application
    {
        static App()
        {
            // this fails:
            Database.SetInitializer(new RecreateDatabaseIfModelChanges<NerdDinners>());

            // The type or namespace name 'RecreateDatabaseIfModelChanges'
            // could not be found (are you missing a using directive or
            // an assembly reference?)                

            using (var nerdDinners = new NerdDinners())
            {
                var dinner = new Dinner()
                {
                    Title = "Party at Scott's House",
                    EventDate = DateTime.Parse("12/31/2010"),
                    Address = "Building 40", …
Run Code Online (Sandbox Code Playgroud)

wpf entity-framework ef-code-first entity-framework-4.1

27
推荐指数
1
解决办法
8746
查看次数

在EF 4.3中使用参数执行存储过程时,对象类型System.Collections.Generic.List中不存在映射

最近我一直在处理存储过程并遇到一个奇怪的问题.

首先,我能够通过以下方式从数据库成功调用存储过程:

IList <XXXViewModel> XXXList = _context.Database.SqlQuery("spXXX").ToList();

但是当我需要传递参数时,它失败了:

var parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("param1", param1Value));
parameters.Add(new SqlParameter("param2", param2Value));
IList<XXXViewModel> XXXList =
_context.Database.SqlQuery<XXXViewModel>("spXXX @param1, @param2", parameters).ToList();
Run Code Online (Sandbox Code Playgroud)

我得到了ff,错误:

从对象类型System.Collections.Generic.List`1 [[System.Data.SqlClient.SqlParameter,System.Data,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]到已知的托管提供者本机不存在映射类型.

请注意,我也尝试过:

_context.Database.ExecuteSqlCommand<EXEC XXXViewModel>("spXXX @param1, @param2", parameters).ToList();
Run Code Online (Sandbox Code Playgroud)

但得到了相同的结果:-(.

我也尝试通过指定每个参数来调用:

IList<XXXResult> query = Context.Database.SqlQuery<XXXResult>("SP @paramA, @paramB, @paramC", new SqlParameter("paramA", "A"), new SqlParameter("paramB", "B"), new SqlParameter("paramC", "C")).ToList();
Run Code Online (Sandbox Code Playgroud)

任何人有任何想法?

entity-framework ef-code-first entity-framework-4.1

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

实体框架迁移错误 - 序列不包含任何元素

命令: add-migration blahblah -verbose
错误:序列不包含任何元素

在收到此错误之前我做了一些事情.我对代码优先模型进行了更改,但尚未运行add-migration.然后我添加了一个EDMX模型来直观地展示一个想法.我意识到EDMX模型弄乱了我的代码所以我删除了它.我试图运行add-migration并得到"序列不包含任何元素".我升级到EF 5并卸载旧的Migrations包,但我的配置除外.然后我add-migration再次尝试,我仍然得到"序列不包含任何元素".以下是错误的其余部分.

System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GetQualifiedTableName(XDocument model, String entitySetName)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<FindRenamedIndependentAssociationColumns>b__ba(<>f__AnonymousType16`2 <>h__TransparentIdentifieraa)
   at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
   at System.Linq.Enumerable.<DistinctIterator>d__81`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString)
   at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Sequence contains no elements
Run Code Online (Sandbox Code Playgroud)

.net entity-framework ado.net-entity-data-model edmx ef-code-first

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

使用DbContext设置<T>()而不是在上下文中公开

执行以下操作时是否存在任何差异:

public class UsersContext : DbContext
{
    public DbSet<User> Users { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

与使用Set<T>上下文的方法:

public class UsersContext : DbContext
{
}

var db = new UsersContext();
var users = db.Set<User>();
Run Code Online (Sandbox Code Playgroud)

这些有效地做了同样的事情,给了我一组用户,但是除了你没有通过属性暴露集合之外还有什么大的差异吗?

.net c# entity-framework ef-code-first dbcontext

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

如何在使用EF代码的.SaveChanges()期间记录所有实体更改?

使用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)

我用这种方式使用它: …

c# logging repository ef-code-first dbcontext

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

实体框架代码优先 - 定义此EntityType的密钥

嗨,我打算在我的一个项目中测试EF Code First.这就是我想要的.我有三张桌子,结构如下

public partial class App_user
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email_address { get; set; }
        public string password { get; set; }
        public int user_type { get; set; }
        public List<Role> Roles { get; set; }
    }

public partial class Role
    {
        public int id { get; set; }
        public string name { get; set; }
    }
public partial class User_role
    {
        public int user_id { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first entity-framework-4.1

26
推荐指数
2
解决办法
7万
查看次数

Linq使用GroupBy时,Include无效

包括matchparticipants不工作.我在调试时总是说Null.但是,当我将GroupBy置于评论中时,它可以正常工作.我使用实体框架4.3.1与代码优先.

实体:

public class Match
    {
        [ScaffoldColumn(false)]
        public int MatchId { get; set; }

        [Required(ErrorMessage = "Matchtype is a required field")]
        public int Scheme { get; set; }

        [Required]
        [DefaultValue(false)]
        public bool Finished { get; set; }

        public int Round { get; set; }


        // Relations
        [Required]
        public Category Category { get; set; }

        public Official Official { get; set; }

        public Slot Slot { get; set; }

        public ICollection<MatchParticipant> MatchParticipants { get; set; }
    }

 public class MatchParticipant
    { …
Run Code Online (Sandbox Code Playgroud)

linq-to-entities ef-code-first entity-framework-4.3

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