我试图为具有两个关键属性的模型类型定义一个键,并定义如下:
type Model () =
member val IdOne = 0 with get, set
member val IdTwo = 0 with get, set
member val OtherProperty = "" with get, set
Run Code Online (Sandbox Code Playgroud)
当我尝试在Entity Framework 5中使用此模型时,我收到错误"模型没有定义键.定义此EntityType的键".给出了模型类型,我无法更改它们并添加[<Key>]属性.所以我尝试了Fluent API.
在C#中,你会做这样的事情:
modelBuilder.Entity<Model>().HasKey(m => new { m.IdOne, m.IdTwo });
Run Code Online (Sandbox Code Playgroud)
它使用匿名类型.但对于我的生活,我无法弄清楚如何在F#中解决这个问题.我尝试了Tuples,Records,甚至是具有IdOne和IdTwo属性的常规类型:
// Regular type with properties IdOne & IdTwo.
type ModelKey (idOne, idTwo) =
member this.IdOne = idOne
member this.IdTwo = idTwo
modelBuilder.Entity<Model>().HasKey(fun (m : Model) -> ModelKey (m.IdOne, m.IdTwo))
// ArgumentNullException: Value cannot be null. Parameter …Run Code Online (Sandbox Code Playgroud) 我有两个型号,一个用于设备类型,另一个用于设备问题.这里的关系是一个(DeviceType)到很多(DeviceIssues).以下是模型:
public class DeviceType : ModelBase
{
public string Manufacture { get; set; }
public DeviceTypes Type { get; set; }
public string Model { get; set; }
public virtual ICollection<DeviceIssue> Issues { get; set; }
}
public class DeviceIssue : ModelBase
{
public string Description { get; set; }
public decimal RepairCost { get; set; }
public int DeviceTypeId { get; set; }
public virtual DeviceType DeviceType { get; set; }
}
public abstract class ModelBase
{
public int …Run Code Online (Sandbox Code Playgroud) File C:\Users\Acer\Desktop\Projelerim\BEM_CANLI\BEM\packages\EntityFramework.5.
0.0\tools\init.ps1 cannot be loaded because its execution is blocked by softwar
e restriction policies. For more information, contact your system administrator.
At line:1 char:44
+ $__pc_args=@(); $input|%{$__pc_args+=$_}; & <<<< 'C:\Users\Acer\Desktop\Proj
elerim\BEM_CANLI\BEM\packages\EntityFramework.5.0.0\tools\init.ps1' $__pc_args[
0] $__pc_args[1] $__pc_args[2]; Remove-Variable __pc_args -Scope 0
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
Run Code Online (Sandbox Code Playgroud)
我在包管理器控制台中遇到上述错误.我找到了一些解决方案,但我无法修复它.我试过以下
我改变了执行政策,

但我总是得到同样的错误.
我试图在asp.net身份中自定义IdentityUser类.
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
IsBlocked = false;
}
public bool IsBlocked { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
问题是:使用代码优先迁移时,附加字段创建为可空.如果我删除数据库并重新创建它也是一样的.
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[UserName] NVARCHAR (MAX) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[IsConfirmed] BIT NOT NULL,
[IsBlocked] BIT NULL,
[Discriminator] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我在同一个DbContext上的其他类中有布尔字段,并且它们都是非空的(因为它们应该).
entity-framework ef-code-first entity-framework-6 asp.net-identity
我正在使用Entity Framework 6开发代码优先数据库.
我知道我可以设置[MaxLength(myLen)]模型的属性.
我想知道的是,如果可以在过滤器或自定义属性中执行此操作,那么所有字符串都采用默认值,即250,除非直接在属性上指定.
如果做不到这个,有没有办法改变默认值nvarchar(max)?
我的应用程序是使用.Net 4.0 Client Profile,WPF,EF 6.0,Code-First,2 dataproviders(SQL Server CE和MySQL)创建的.
如果我将调试/发布文件夹复制到本地文件路径,它可以正常工作.
但如果将此文件夹复制到pendrive或放入服务器文件夹中.我第一次需要创建DbContext或调用数据库初始化程序时,它失败并出现以下错误(J:是我的pendrive):
'System.Data.Entity.Internal.AppConfig'的类型初始值设定项引发异常创建实体框架的配置节处理程序时发生错误:请求失败.(J:\USERS\George\Release\app.Config第6行)
System.TypeInitializationException: System.Void DG.Apps.JLM.Questoes.Presentation中'System.Data.Entity.CreateDatabaseIfNotExists 1 [DG.Apps.JLM.Questoes.Domain.Contexts.QuestoesContext] .. ctor()的类型初始值设定项.App.OnStartup(System.Windows.StartupEventArgs e)位于j:\ USERS\George\Release\Common\Common\Apps\JLM\DG.Apps.JLM.Questoes\DG.Apps.JLM.Questoes.Presentation\App. xaml.cs: Object System.Windows.Application的第30行.<.ctor> b__1(System.Object unused) 在Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback,System.Object args,System.Int32) numArgs) at System MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object source,System.Delegate方法,System.Object args,System.Int32 numArgs,System.Delegate catchHandler) System.Void System.Windows.Threading.DispatcherOperation 静态System.Void系统 上的静态System.Void System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object状态)中的.InvokeImpl().Threading.ExecutionContext.runTryCode(System.Object userData) 静态System.Void System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback回调,System.Object状态) 静态System.Void系统.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback回调,System.Object状态,System.Boolean ignoreSyncCtx) 静态System.Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext) ,System.Threading.ContextCallback回调,System.Object状态) , 位于System.Void的System System.Windows.Threading.DispatcherOperation.Invoke()System.Windows.Threading.Dispatcher.ProcessQueue(), 位于IntPtr System.Windows.Threading.Dispatcher .WndProcHook(System.IntPtr hwnd,System.Int32 msg,System.IntPtr wParam,System.IntPtr lParam,System.Boolean处理) 在IntPtr MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd,System.Int32 msg,System.IntPtr wParam,System.IntPtr …
我一直在项目中首先使用Entity框架代码,并且之前已经创建/修改了所有表.现在我需要为表添加一个唯一约束.我想创建一个迁移,该Up()方法将包含以下行.并且模型类没有变化.
CreateIndex("TableName", new[] { "Column1" }, true, "IX_UniqueKey");
Run Code Online (Sandbox Code Playgroud)
也许是下面的一行 Down()
DropIndex("TableName", new [] { "Column1" });
Run Code Online (Sandbox Code Playgroud)
我可以在迁移文件夹下手动创建一个文件吗?是否有任何强制性文件名约定?
如何创建.Designer.cs和.resx文件?或者我应该使用add-migration一些参数?
c# sql-server entity-framework ef-code-first entity-framework-6
我有一个非常奇怪的情况,特别是一个班级.将类添加到DbContext要插入数据库并调用Db.SaveChanges代码后,首先/ ef不会将主键id分配回类.
这真的很奇怪,我以前从未遇到过这种情况,我似乎无法在网上找到任何解决方案.
这是代码目前的样子......
发票代码头等舱
[Table("invoice")]
public class Invoice
{
[Key]
[Column("invoice_id")]
public int InvoiceId { get; set; }
[Column("invoice_credit_card_payment_id")]
public int InvoiceCreditCardPaymentId { get; set; }
[Column("batch_id")]
public int BatchId { get; set; }
[Column("customer_id")]
public int CustomerId { get; set; }
etc.....
}
Run Code Online (Sandbox Code Playgroud)
将发票插入数据库的代码
var invoice = new Invoice()
{
BatchId = 0,
Memo = "",
PayDateTime = DateTime.Now,
QuickbooksAccountName = "",
QuickbooksId = "",
Terms = "",
etc....
};
DbContext.Current.Invoices.Add(invoice);
//Invoice record does …Run Code Online (Sandbox Code Playgroud) 我想在现有表中添加一个非null,foreing键列.
环境:EF 6,代码优先,基于代码的迁移
//Code from Migration class for new entity Currency
CreateTable("dbo.Currency",
c => new
{
CurrencyID = c.Int(nullable: false, identity: true),
Code = c.String(nullable: false, maxLength: 3, fixedLength: true, unicode: false),
Denomination = c.String(nullable: false, maxLength: 50, unicode: false),
})
.PrimaryKey(t => t.CurrencyID);
AddColumn("dbo.Collection", "CurrencyID", c => c.Int(nullable: false));
//Code from Seed() method in Configuration class
context.Currencies.AddOrUpdate(
new Currency
{
Code = "USD",
Denomination = "Dollar"
}
);
//Here i get an exception. Collection is the existing table
context.Database.ExecuteSqlCommand( …Run Code Online (Sandbox Code Playgroud) Database First(和Model First)方法有这个不错的DbContext Generator脚手架文件来生成上下文和模型; Model.Context.tt + Model.tt
由于它们具有内置辅助方法来检索导航属性,因此将它们用于其他目的也非常方便,例如创建控制器,视图等.ASP.NET Scaffolding也做类似的工作但在这种情况下,脚手架需要相反,这些T4文件会同时生成所有文件.
但是,它们仅使用"edmx"文件作为输入.是否可以使用/转换它们用于Code First方法?
实体框架版本6.1
ef-code-first ×10
entity-framework ×10
c# ×5
app-config ×1
asp.net ×1
asp.net-mvc ×1
database ×1
f# ×1
f#-3.0 ×1
powershell ×1
sql-server ×1
t4 ×1
wpf ×1