我在Global.asax中使用以下代码:
DbDatabase.SetInitializer<MyDBContext>
(new DropCreateDatabaseIfModelChanges<MyDBContext>());
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.
虽然我的模型已经改变,我试图使用其中一个新添加的表,但它只是说找不到表.
Invalid object name 'dbo.TableName'.
Run Code Online (Sandbox Code Playgroud)
但是,如果我运行它,它似乎工作,并且正在创建表:
DbDatabase.SetInitializer<MyDBContext>(new DropCreateDatabaseAlways<MyDBContext>());
Run Code Online (Sandbox Code Playgroud)
它确实更新了我的数据库.
我做错了什么?
我在我的开发框上有一个名为ApplicationName_Development的数据库在SQL Server 2008 R2 Developer Edition上运行.
我没有问题地将.NET成员资格表添加到数据库中.当我试图让Code First工作时,我收到以下错误消息:
服务器遇到处理请求的错误.异常消息是"无法检查模型兼容性,因为数据库不包含模型元数据.请确保已将IncludeMetadataConvention添加到DbModelBuilder约定中.
经过一些谷歌搜索,我发现我必须删除数据库,让EF创建数据库.这没关系,但我丢失了所有的.NET会员表.我可以返回并再次添加成员资格表,但如果我的模型更改并且EF需要重新创建数据库,那么我必须再次添加成员资格表.
我该如何解决这个问题?
我正在尝试使用EF 4.1构建枚举问题的通用解决方案.我的解决方案基本上是如何在ef 4中伪造枚举的通用版本.枚举包装类在其余代码中运行得非常好,并允许以下代码:
EnumWrapper<Color> c = Color.Red;
Run Code Online (Sandbox Code Playgroud)
这是枚举包装类:
public class EnumWrapper<TEnum> where TEnum : struct, IConvertible
{
public EnumWrapper()
{
if (!typeof(TEnum).IsEnum)
throw new ArgumentException("Not an enum");
}
public TEnum Enum { get; set; }
public int Value
{
get { return Convert.ToInt32(Enum); }
set { Enum = (TEnum)(object)value; }
}
public static implicit operator TEnum(EnumWrapper<TEnum> w)
{
if (w == null) return default(TEnum);
else return w.Enum;
}
public static implicit operator EnumWrapper<TEnum>(TEnum e)
{
return new EnumWrapper<TEnum>() …Run Code Online (Sandbox Code Playgroud) 自从4.1(现在我在4.3)以来,我一直在努力解决这个问题.在我看来,要调用种子方法,我应该做的就是以下内容:
1)在sqlserver上创建一个空数据目录2)执行以下代码:
Database.SetInitializer(new DropCreateDatabaseAlways<SiteDB>());
Run Code Online (Sandbox Code Playgroud)
我的SiteDB定义如下:
public class SiteDBInitializer :
DropCreateDatabaseAlways<SiteDB>
{
protected override void Seed(SiteDB db)
{
... (break point set here that never gets hit)
Run Code Online (Sandbox Code Playgroud)
我觉得我必须错过一些非常简单的东西,因为这会创建我的表,但绝不会调用种子方法.
为了更清楚,这是一个包含所有代码的完整示例.当我运行它时,种子永远不会被调用:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace ConApp
{
internal class Program
{
private static void Main(string[] args)
{
Database.SetInitializer(new SiteDBInitializer());
using (var db = new SiteDB())
{
var x = db.Customers;
}
}
}
public class SiteDB : DbContext
{
public DbSet<Customer> Customers { get; …Run Code Online (Sandbox Code Playgroud) entity-framework code-first entity-framework-4 ef-code-first
我正在使用实体框架并建模多对多关系.
我使用流畅的API创建了两个实体之间的关系(假设用户和组):
this.HasMany(t => t.Users)
.WithMany(t => t.Groups)
.Map(
m =>
{
m.ToTable("GroupMembers");
m.MapLeftKey("Group_Id");
m.MapRightKey("User_Id");
});
Run Code Online (Sandbox Code Playgroud)
这很好用,但我也希望能够直接引用GroupMembers表.要做到这一点,我有类似的东西:
[Table("GroupMembers")]
public class GroupMember
{
#region Properties
/// <summary>
/// Gets or sets the group.
/// </summary>
public virtual Group Group { get; set; }
/// <summary>
/// Gets or sets the Id of rht group.
/// </summary>
[Key]
[Column("Group_Id", Order = 1)]
public int GroupId { get; set; }
/// <summary>
/// Gets or sets the user.
/// </summary>
public virtual User …Run Code Online (Sandbox Code Playgroud) 我正在使用实体框架,我需要检查名称="xyz"的产品是否存在...
我想我可以使用Any(),Exists()或First().
对于这种情况,哪一个是最好的选择?哪一个性能最好?
谢谢,
米格尔
我确信我在这里遗漏了一些简单的东西.我正在尝试遵循Code First Entity Framework教程,该教程告诉我使用一些数据注释.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace Model
{
public class Destination
{
public int DestinationId { get; set; }
[Required]
public string Name { get; set; }
public string Country { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[Column(TypeName="image")]
public byte Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
编译器对前两个注释没有任何问题,但它似乎不喜欢:[Column(TypeName="image")].
错误:
找不到类型或命名空间名称"列".
找不到类型或命名空间名称"ColumnAttribute".
我正在使用Visual Studio 2012和Entity Frameworks …
我在EF 5.0中迈出了第一步,我有很多关系.电影可以有多种类型和类型可以有多个电影
public class Movie
{
public int MovieId { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Type> Types { get; set; }
}
public class Type
{
public int TypeId { get; set; }
public string MovieType { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我生成数据库时,它会在Movie和type之间创建多对多
那么,我应该怎样做才能将这个多表种子播种?我在这里尝试了很多解决方案,但它没有用.
此外,这是使用EF代码首先生成多对多关系的最佳方法
我正在尝试启用迁移,但它正在抛出异常:
检查上下文是否以现有数据库为目标... System.TypeInitializationException:'System.Data.Entity.Migrations.DbMigrationsConfiguration`1'的类型初始值设定项引发异常.---> System.TypeInitializationException:'System.Data.Entity.Internal.AppConfig'的类型初始值设定项引发了异常.---> System.Configuration.ConfigurationErrorsException:配置系统初始化失败---> System.Configuration.ConfigurationErrorsException:必须在'section'标签上指定'name'属性.
我假设App.config文件没有正确设置(当我添加EF包时它自动设置).我所做的就是添加连接字符串:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section Name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings>
<add Name="MyContext" connectionString="data source=MYSERVER;initial catalog=CodeFirstTest;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我正在使用SQL Server 2008 R2.
因为我有一个连接字符串,我不相信我需要defaultconnectionfactory.我对么?(注意:即使没有本节,我仍然会得到相同的例外)
我还缺少什么?
如何通过断点进入OnModelCreating并查看我的逻辑是否错误或者模型构建器是否正在做我不期望的事情?我已经看到很多关于如何调试实际迁移的帖子,但没有关于如何观察模型代码生成方式的内容.
我正试图在我的一些实体上实现一些自定义属性,并且它被忽略了; 我想知道我的配置正在做什么,因为它正在生成模型代码.
c# entity-framework database-migration ef-code-first entity-framework-core