标签: ef-code-first

.WithMany()和.WithOptional()之间的区别?

以下是两种类似的流畅API配置:

和很多()

modelBuilder.Entity<Country>()
            .HasRequired(cou => cou.Currency)
            .WithMany()
            .WillCascadeOnDelete(false); 
Run Code Online (Sandbox Code Playgroud)

WithOptional()

modelBuilder.Entity<Country>()
            .HasRequired(cou => cou.Currency)
            .WithOptional()
            .WillCascadeOnDelete(false);
Run Code Online (Sandbox Code Playgroud)

我在这里要表达的是:每个都Country需要具体Currency,但是Currency可以分配给零,一个或多个国家.

我必须使用上述哪一项陈述?或者换句话说:究竟有什么区别.WithMany().WithOptional()运营商?

entity-framework fluent-interface ef-code-first

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

Entity Framework 4.1代码第一种方法:如何定义属性的长度

正如标题暗示:

怎么可能在代码第一种方法中告诉实体框架4.1,我确实希望某些属性(特别是类型字符串)的长度为256或nvarchar(max),或者......

所以,如果这是我的模型

public class Book{
   public string Title { get; set; } //should be 256 chars
   public string Description {get;set} //should be nvarchar(max)
}
Run Code Online (Sandbox Code Playgroud)

怎么定义?

提前致谢!

mapping ef-code-first entity-framework-4.1

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

使用mvc-mini-profiler数据库分析与Entity Framework Code First

我正在使用ASP.Net MVC 3和Entity Framework代码优先构建的项目中使用mvc-mini-profiler.

一切都很有效,直到我尝试通过ProfiledDbConnection在文档中描述的方式包装连接来添加数据库分析.由于我使用的是DbContext,因此我尝试提供连接的方式是使用静态工厂方法构造函数:

public class MyDbContext : DbContext
{                
    public MyDbContext() : base(GetProfilerConnection(), true)
    { }

    private static DbConnection GetProfilerConnection()
    {
        // Code below errors
        //return ProfiledDbConnection.Get(new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString));

        // Code below works fine...
        return new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString);
    }

    //...
}
Run Code Online (Sandbox Code Playgroud)

使用时ProfiledDbConnection,我收到以下错误:

ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.

堆栈跟踪:

[ArgumentException: The connection is not of type 'System.Data.SqlClient.SqlConnection'.]
   System.Data.SqlClient.SqlProviderUtilities.GetRequiredSqlConnection(DbConnection connection) +10486148
   System.Data.SqlClient.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +77
   System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +44

[ProviderIncompatibleException: The provider did not …
Run Code Online (Sandbox Code Playgroud)

ef-code-first entity-framework-ctp5 mvc-mini-profiler

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

实体框架代码中的SQL"时间"类型

我正在尝试使用Entity Framework Code First在表中创建'time(7)'列.这是我的实体:

public class ShiftDetail
{
    public long Id { get; set; }

    [Required]
    public int DayOfWeek { get; set; }

    [Required]
    [Column(TypeName="time")]
    public DateTime StartTime { get; set; }

    [Required]
    [Column(TypeName = "time")]
    public DateTime EndTime { get; set; }

    public long ShiftId { get; set; }
    public virtual Shift Shift { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试将StartTime和EndTime列的数据库类型设置为"time",但是我收到此错误:

(112,12):错误2019:指定的成员映射无效.类型'ShiftDetail'中成员'StartTime'的类型'Edm.DateTime [Nullable = False,DefaultValue =,Precision =]'与'SqlServer.time [Nullable = False,DefaultValue =,Precision = 7]'不兼容'CodeFirstDatabaseSchema.ShiftDetail'类型中的成员'StartTime'.

我也试过TypeName ="time(7)"但我得到了另一个错误:

(104,6):错误0040:类型时间(7)未使用命名空间或别名限定.只有原始类型可以无限制地使用.

如何创建包含代码的时间列?(最好没有流畅的API)

提前致谢.

.net entity-framework code-first entity-framework-4 ef-code-first

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

代码首次尝试创建数据库时出现异常

我已经创建了一个新的ASP.NET MVC 4应用程序,并希望它首先使用代码.但是,如果数据库文件不存在,它似乎最初不会创建数据库文件.如果我从App_Data文件夹中删除.mdf文件,那么当应用程序尝试访问数据库时,我会收到以下异常:

System.Data.SqlClient.SqlException: Cannot attach the file '<path-to-db-file>.mdf' as database '<my-db-file-name>'.
Run Code Online (Sandbox Code Playgroud)

如果我在调试器中的应用程序中运行它,那么我可以看到在调用LazyInitializer.EnsureInitialized时在InitializeSimpleMembershipAttribute :: OnActionExecuting方法中发生异常.被捕获的例外是:

[System.Reflection.TargetInvocationException]   {"Exception has been thrown by the target of an invocation."}   System.Reflection.TargetInvocationException
Run Code Online (Sandbox Code Playgroud)

内部例外:

[System.InvalidOperationException]  {"The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588"} System.InvalidOperationException
Run Code Online (Sandbox Code Playgroud)

然后我将上面提到的第一个例外作为内部例外.

我有什么想法我做错了吗?

更新

我刚刚尝试使用全新的MVC4应用程序.我可以通过执行以下操作来复制它:

  1. 在VS向导中创建MVC应用程序.
  2. 首次运行应用程序并转到登录页面(注意现在生成mdf文件).
  3. 删除mdf文件,然后返回登录页面.现在抛出异常.

c# asp.net-mvc entity-framework entity-framework-4 ef-code-first

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

如何使用Entity Framework Code-First方法将double []数组存储到数据库

如何使用Entity Framework Code-First将数组的双精度数据存储到数据库中,而不影响现有代码和体系结构设计?

我查看了Data Annotation和Fluent API,我还考虑将double数组转换为字节字符串,并将该字节存储到自己的列中的数据库中.

我无法使用public double[] Data { get; set; }Fluent API 访问该属性,我得到的错误消息是:

该类型double[]必须是非可空值类型才能将其用作参数"T".

Data存储的类成功存储在数据库中,以及与此类的关系.我只是错过了Data专栏.

.net c# entity-framework fluent-interface ef-code-first

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

将ASP.NET标识集成到现有的DbContext中

我正在使用VS2013,.NET 4.5.1中的ASP.NET MVC 5项目,该项目使用Entity Framework 6 Code-First.我有一个体面的数据库建立和有点工作(项目大约两周).我想现在整合用户身份验证,但我不知道如何处理它.在花费了大部分时间进行研究之后,我决定为新的ASP.NET身份框架提供一个必须编写自定义成员资格或角色提供程序的镜头.我感到困惑的是如何使用现有的数据库/模型完成所有工作.

目前,我有一个名为" Employee持有基本员工信息" 的对象(暂时).在整天思考了这个问题之后,我决定将身份验证与它分离成一个User对象,这正是Identity想要的.这就是说我如何使它全部工作?

这是我的Employee班级:

public class Employee : Person {
    public int EmployeeId { get; set; }
    public byte CompanyId { get; set; }
    public string Name {
        get {
            return String.Format("{0} {1}", this.FirstName, this.LastName);
        }
    }
    public string Password { get; set; }
    public bool IsActive { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual Company Company { get; set; }
    public …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first entity-framework-6 asp.net-identity

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

实体框架 - 外键组件...不是类型的声明属性

我有以下模型

public class FilanthropyEvent :  EntityBase, IDeleteable
{  
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime EventDate { get; set; }
    public string Description { get; set; }
    public decimal Target { get; set; }
    public decimal EntryFee { get; set; }
    public bool Deleted { get; set; }

    public ICollection<EventAttendee> EventAttendees { get; set; }
}

public class Attendee : EntityBase, IDeleteable
{
    public int Id { get; set; }
    public …
Run Code Online (Sandbox Code Playgroud)

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

24
推荐指数
1
解决办法
9881
查看次数

如何使用Identity ASP.NET MVC 6使用代码优先迁移为用户和角色设定种子

我创建了一个新的干净的asp.net 5项目(rc1-final).使用身份验证我只需要具有以下代码的ApplicationDbContext.cs:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        // On event model creating
        base.OnModelCreating(builder);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意ApplicationDbContext使用IdentityDbContext而不是DbContext.

有任何IdentityConfig.cs.我需要将经典受保护的覆盖void Seed用于创建角色和用户(如果它不存在)?

seeding ef-code-first asp.net-identity asp.net-core-mvc

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

实体框架中的外键4.1

我正在使用Entity Framework 4.1并使用外键的数据注释.我想知道如何定义产品和类别之间的一对多关系.我想要分类.categoryId with product.cid

public class Category
{
    public string CategoryId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string CId { get; set; }

    public virtual Category Category { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)

请建议

.net data-annotations ef-code-first entity-framework-4.1

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