我正在尝试将我的存储库更新为EF5,但遇到了一些错误.我已经看了一下stackoverflow,因为类似的错误发现了一些问题/答案,但不幸的是,相同的答案并没有解决我的问题.
这是我的错误:
The entity type User is not part of the model for the current context.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Run Code Online (Sandbox Code Playgroud)
这是我的DbContext类:
public abstract class WebModelContext : DbContext
{
public WebModelContext()
: base("WebConnection")
{
Configuration.LazyLoadingEnabled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是继承我的WebModelContext类的上下文类:
public class AccountContext : WebModelContext
{
private DbSet<User> _users;
public AccountContext()
: …Run Code Online (Sandbox Code Playgroud) 我是新手.我不知道如何使用SqlSpatialFunction MakeValid.我有一个DbGeometry,它是一个多边形.此多边形无效,我希望使其有效.
任何人都可以解释如何使用MakeValid方法?
谢谢 !:d
我有2个POCO课程(如下),并希望删除他们的两个记录之间的链接.根据这个 EF 5.0应该能够处理删除而不加载User类,如下所示:
context.Computers.Find("test").User = null;
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
这不起作用,但使用.net 4批准的方法,它的工作原理:
en = context.Computers.Find("test");
context.Entry(en).Reference(e => e.User).Load();
en.User = null;
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
我的EF参考是EntityFramework.dll版本5.0.0.0.我错过了一些明显的东西吗?
以下是课程:
public class Computer
{
public string Id { get; set; }
public Nullable<int> UserId { get; set; }
public virtual User User { get; set; }
}
public class User
{
public int Id { get; set; }
public virtual ICollection<Computer> Computers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
编辑:以下链接文章中的具体行似乎与我看到的功能不一致:
要删除关系,请将navigation属性设置为null.如果您正在使用基于.NET 4.0的实体框架,则需要在将其设置为null之前加载相关的结尾.例如:
context.Entry(course).Reference(c => c.Department).Load();
course.Department = null;
Run Code Online (Sandbox Code Playgroud)
从基于.NET 4.5的Entity …
我创建了一个全新的Web API项目,创建了一个简单的Code First模型(一个类的id和的DbContext对象,这就是它),并在包管理器控制台运行启用的迁移.
我注意到它在SQLEXPRESS而不是LocalDB中创建了数据库,尽管DefaultConnection字符串指向Web.config文件中的(LocalDB).这会导致后续查询失败,声称数据库尚未初始化.
如何在VS 2012中将Enable-Migrations命令指向LocalDB而不是SQLExpress?我已经尝试安装SQL Management Studio 2012 Express并停止SQLEXPRESS数据库,但这只会导致Enable-Migration命令失败.
有小费吗?
注意:我安装了VS 2010,以及它附带的所有默认软件(如SQL Server),所以这可能是干扰.
sql-server-express entity-framework-5 localdb visual-studio-2012
我正在使用Entity Framework 5,Code首先针对SQL 2005数据库.我有一个存储库,有一个执行存储过程的方法 - 该方法如下所示;
public IEnumerable<PossibleDuplicateCustomer> GetPossibleDuplicates(Customer customer)
{
return DbContext.Database.SqlQuery<PossibleDuplicateCustomer>(
"EXEC SearchPotentialDuplicates @CustomerId = {0}, @FirstName = {1}, @LastName = {2}, @dob = {3}",
customer.CustomerId,
customer.CustomerFirstName,
customer.CustomerLastName,
customer.Dob);
}
Run Code Online (Sandbox Code Playgroud)
我尝试的另一个变种是;
public IEnumerable<PossibleDuplicateCustomer> GetPossibleDuplicates(Customer customer)
{
return DbContext.Database.SqlQuery<PossibleDuplicateCustomer>(
"SearchPotentialDuplicates @CustomerId, @FirstName, @LastName, @dob",
new SqlParameter("CustomerId", customer.CustomerId),
new SqlParameter("FirstName", customer.CustomerFirstName),
new SqlParameter("LastName", customer.CustomerLastName),
new SqlParameter("dob", customer.Dob));
}
Run Code Online (Sandbox Code Playgroud)
当我执行此操作时 - 我收到错误;
System.Data.SqlClient.SqlException(0x80131904):'SearchPotentialDuplicates'附近的语法不正确.
所以我用miniprofiler抓住了生成的sql - 这给了我;
DECLARE @p0 int = 12644,
@p1 nvarchar(4) = N'adam',
@p2 nvarchar(3) = N'ant',
@p3 datetime …Run Code Online (Sandbox Code Playgroud) t-sql sql-server stored-procedures mvc-mini-profiler entity-framework-5
我正在尝试Customer使用ASP.NET Web API和Entity Framework 5代码优先更新我的数据库,但它无法正常工作.我的实体看起来像这样:
public class CustomerModel
{
public int Id { get; set; }
public string Name { get; set; }
// More fields
public ICollection<CustomerTypeModel> CustomerTypes { get; set; }
}
public class CustomerTypeModel
{
public int Id { get; set; }
public string Type { get; set; }
[JsonIgnore]
public ICollection<CustomerModel> Customers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
没有什么特别的.我已经构建了一个Web界面,用户可以通过提供名称和检查一个或多个客户类型来添加客户.点击提交按钮时,数据将发送到我的Web API方法:
public void Put([FromBody]CustomerModel customer)
{
using (var context = new MyContext())
{
context.Customers.Attach(customer);
context.Entry(customer).State = …Run Code Online (Sandbox Code Playgroud) many-to-many ef-code-first asp.net-web-api entity-framework-5
我有一个使用实体框架的MVC 3网站,它刚刚开始崩溃特定的操作.我希望我有更多的信息,但我可以从Windows事件查看器中获取的是:
Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7a5f8
Faulting module name: System.Data.Entity.ni.dll, version: 4.0.30319.1, time stamp: 0x4ba1e2fd
Exception code: 0xc00000fd
Fault offset: 0x003aac6a
Faulting process id: 0x2f0
Faulting application start time: 0x01ce540a70477360
Faulting application path: C:\Windows\SysWOW64\inetsrv\w3wp.exe
Faulting module path: C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Data.Entity\dc0c74bc42bbaeffcb7158c7ed0f1653\System.Data.Entity.ni.dll
Report Id: 3659a820-bffe-11e2-8207-404094d3cf82
Run Code Online (Sandbox Code Playgroud)
如果我停止IIS,删除本机映像,然后重新启动IIS一切正常.然而,一两天之后问题就会回来.
有没有人有这方面的想法?好像在自动创建的图像中发生了某些事情ngen.exe,但我不知道从哪里开始调试.谢谢!
我终于能够捕获到故障的完整内存转储.这是核心的堆栈溢出问题.这是堆栈跟踪,重复位...
System.Data.Query.InternalTrees.BasicOpVisitorOfT`1[[System.__Canon, mscorlib]].VisitNode(System.Data.Query.InternalTrees.Node) 086a9990 0609755c 0f16e1a0 59c4a149
System.Data.Query.InternalTrees.NodeInfoVisitor.RecomputeNodeInfo(System.Data.Query.InternalTrees.Node) 01f8f258 0609755c 06412d6c 0f16e1ec
System.Data.Query.InternalTrees.Node.InitializeNodeInfo(System.Data.Query.InternalTrees.Command) 086a97f0 59c64fb9 0f16e1b8 5a1d255e
System.Data.Query.InternalTrees.Node.GetExtendedNodeInfo(System.Data.Query.InternalTrees.Command) 06412da4 0f16e204 59c64ef1 06412da4
System.Data.Query.InternalTrees.BasicOpVisitorOfT`1[[System.__Canon, mscorlib]].Visit(System.Data.Query.InternalTrees.OuterApplyOp, System.Data.Query.InternalTrees.Node) 06412da4 0f16e220 59c12799 5996fb84 …Run Code Online (Sandbox Code Playgroud) 我有一个用户角色多对多关系,这个摘录是从EntityTypeConfiguration<Role>派生类中指定的,它允许我为单个表指定模式,例如:
[Schema(SchemaNames.ParkPay)]
class WeekDayConfig : EntityTypeConfigurationWithSchema<WeekDay>
{
internal WeekDayConfig()
{
Ignore(t => t.IsDeleted);
Property(p => p.Name)
.IsRequired()
.HasMaxLength(20);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,for,for Role配置类包含此代码,结果表UserRole在'dbo'模式下创建,而不是我想要的模式.这是代码:
[Schema(SchemaNames.ParkPay)]
internal class RoleConfig : EntityTypeConfigurationWithSchema<Role>
{
public RoleConfig()
{
HasMany(t => t.Users)
.WithMany(t => t.Roles)
.Map(m =>
{
m.ToTable("UserRole");
m.MapLeftKey("RoleId");
m.MapRightKey("UserId");
});
}
}
Run Code Online (Sandbox Code Playgroud)
有什么我可以做的,除了脚本在初始化的播种阶段更改模式,UserRole在'parkpay'模式下创建表而不是'dbo'模式?
我在Windows上运行PostgreSQL 9.2.
我有一个现有的表,其中包含一些不可为空的列:
CREATE TABLE testtable
(
bkid serial NOT NULL,
bklabel character varying(128),
lacid integer NOT NULL
}
Run Code Online (Sandbox Code Playgroud)
我在这个表上创建了一个视图:
CREATE OR REPLACE VIEW test AS
SELECT testtable.bkid, testtable.lacid
from public.testtable;
Run Code Online (Sandbox Code Playgroud)
我很惊讶视图报告的information_schema.columns对于所选列是否可以为YES?
select * from information_schema.columns where table_name = 'test'
Run Code Online (Sandbox Code Playgroud)
报告:
"MyDatabase";"public";"test";"bkid";1;"";"YES";"integer";;;32;2;0;;"";;"";"";"";"";"";"";"";"";"";"MyDatabase";"pg_catalog";"int4";"";"";"";;"1";"NO";"NO";"";"";"";"";"";"";"NEVER";"";"NO"
"MyDatabase";"public";"test";"lacid";2;"";"YES";"integer";;;32;2;0;;"";;"";"";"";"";"";"";"";"";"";"MyDatabase";"pg_catalog";"int4";"";"";"";;"2";"NO";"NO";"";"";"";"";"";"";"NEVER";"";"NO"
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?
我的问题是我正在尝试在实体框架数据模型中导入此类视图,但它失败了,因为所有列都标记为可为空.
编辑1:
以下查询:
select attrelid, attname, attnotnull, pg_class.relname
from pg_attribute
inner join pg_class on attrelid = oid
where relname = 'test'
Run Code Online (Sandbox Code Playgroud)
回报:
attrelid;attname;attnotnull;relname
271543;"bkid";f;"test"
271543;"lacid";f;"test"
Run Code Online (Sandbox Code Playgroud)
正如所料,attnotnull是'假'.
正如@ Mike-Sherrill-Catcall建议的那样,我可以手动将它们设置为true:
update pg_attribute
set attnotnull = 't'
where …Run Code Online (Sandbox Code Playgroud) 我正在当前解决方案中的单独项目中实现代码优先模式.既然我这样做了,我初始化我的连接字符串,DbContext我得到这个奇怪的错误:
无法确定存储版本; 需要有效的存储连接或版本提示.
我正在使用Entity Framework 5.0.0.0,我的其他解决方案项目工作得很好,因为他们使用EDMX文件进行数据库访问,但只有我的代码第一个项目抛出此异常.它是什么,为什么?
c# ×3
.net-4.5 ×1
dbcontext ×1
iis-7.5 ×1
linq ×1
localdb ×1
many-to-many ×1
postgresql ×1
sql-server ×1
t-sql ×1
view ×1