有谁知道如何在EF4(实体框架4)中更改实体的映射数据库表?
稍后编辑:我想我已经在模型浏览器中找到了定义表名的位置.但他们的名字是只读的,所以不可能使用设计师编辑它们.此外,没有引用(从我搜索过的内容)到xml架构中的表名.
编辑:这只发生在具有存储库的大型项目中.有没有人使用EF4和CodeFirst方法并使用存储库?请建议我.
你好.我目前正在使用EF4 CodeFirst Classes.在我的测试项目中,我有两个类,作者和书(作者得到书).我正在尝试做的是我在我的作者类中有一个AddBook,但似乎不能像我不能将它添加到集合中..这里是我的类和两个不同的例外.
public class Book
{
public virtual int BookId { get; set; }
public virtual string Title { get; set; }
public virtual Author Author { get; set; }
}
public class Author
{
public virtual int AuthorId { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
public Author()
{
Books = new Collection<Book>();
}
public void AddBook(Book book)
{
book.Author = this;
Books.Add(book);
}
} …Run Code Online (Sandbox Code Playgroud) entity-framework code-first entity-framework-4 asp.net-mvc-2
我有两个IQueryables:
成分:
IngId
Description
Run Code Online (Sandbox Code Playgroud)
AvailableIngredient:
IngId
Run Code Online (Sandbox Code Playgroud)
我已经有了一个IQueryable成分:
var ingQuery = from i in context.Ingredients
select i;
Run Code Online (Sandbox Code Playgroud)
如何向他添加AvailableIngredient联接以便过滤(即内部联接)?如果我必须一直加入,我知道如何做到这一点,即从...加入context.Available ...等),但是Join是有条件的,所以我需要使用其他语法:
if (filterByAvailable)
{
IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
ingQuery = ingQuery.Join(...); // Can I use this to join to the query?
}
Run Code Online (Sandbox Code Playgroud)
这可能不是正确的方法,所以这就是我想要做的:
编辑:
这是我目前使用的代码(非常快),但它意味着重复的代码:
IQueryable<Ingredient> query;
if (filterByAvailable)
{
IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
query = from item in context.Ingredients
// Quite a few `where` clauses and stuff
join t in availableQuery on item.IngId equals t.IngId
select item;
} …Run Code Online (Sandbox Code Playgroud) 我知道实体框架工作不允许您使用非主要唯一密钥作为外键关联从数据库生成模型.我可以手动修改EDMX吗?如果是这样,有人可以给我一个例子或参考吗?如果没有,还有其他可能吗?
最简单的例子:
这是表格的DDL.你会注意到我有一个从PersonType.TypeCode到Person.TypeCode的外键
CREATE TABLE [dbo].[PersonType](
[PersonTypeId] [int] NOT NULL,
[TypeCode] [varchar](10) NOT NULL,
[TypeDesc] [varchar](max) NULL,
CONSTRAINT [PK_PersonType] PRIMARY KEY CLUSTERED
([PersonTypeId] ASC)
CONSTRAINT [UK_PersonType] UNIQUE NONCLUSTERED
([TypeCode] ASC)
)
CREATE TABLE [dbo].[Person](
[PersonId] [int] NOT NULL,
[TypeCode] [varchar](10) NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
([PersonId] ASC)
)
ALTER TABLE [dbo].[Person] WITH CHECK ADD CONSTRAINT [FK_Person_PersonType] FOREIGN KEY([TypeCode])
REFERENCES [dbo].[PersonType] ([TypeCode])
ALTER TABLE [dbo].[Person] CHECK CONSTRAINT [FK_Person_PersonType]
Run Code Online (Sandbox Code Playgroud)
这是EDMX生成的
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content …Run Code Online (Sandbox Code Playgroud) c# .net-4.0 visual-studio-2010 sql-server-2008 entity-framework-4
如何在实体框架4中连接字符串我有一个列中的数据,我想将字符串保存为逗号分隔的字符串,如"value1,value2,value3"是否有方法或操作员在EF4中执行此操作?示例:假设我有两列Fruit并Farms具有以下值:
如果我喜欢这个
var dataSource = this.context
.Farms
.Select(f => new
{
f.Id,
Fruits = string.Join(", ", f.Fruits)
});
当然我会得到这个错误
LINQ to Entities无法识别方法'System.String Join(System.String,System.Collections.Generic.IEnumerable`1 [System.String])'方法,并且此方法无法转换为商店表达式.
这有什么解决方案吗?
sql entity-framework aggregate string-concatenation entity-framework-4
是否可以进入linq查询?我有一个linq to entity framework 4查询,它是最简单的形式:
List = List.Where(f => f.Value.ToString().ToLowerInvariant().Contains(filter.ToLowerInvariant()));
Run Code Online (Sandbox Code Playgroud)
这是对实体框架的查询DbContext,我很难理解为什么它适用于以下内容:
列表搜索001产量没有针对以下列表的结果
然而,任何其他搜索产生结果(如t00或Test)
基本上我正在寻找为什么当我使用contains时,如上所述的查询不会返回结果,并且该值匹配字符串的结尾与中间或开头的结果.这真的令人困惑.
好吧,它似乎与某些事情有关ToLowerInvariant()- 当我删除该方法时,它工作得很好.
我的数据库中有一个带有money field的表.我已经为该货币领域创建了实体并创建了小数属性.当该字段的值显示在我的MVC3视图上时,它在小数点后有四个零0000,如下所示:5489.0000.我想只显示两个00或小数位.我该如何解决呢?为什么它显示四位小数,即使我将属性声明为十进制.
请建议.
我正在尝试为Entity Framework实现数据本地化逻辑.因此,如果例如查询选择Title属性,则在幕后它应该引用列Title_enGB或Title_deCH取决于当前用户文化.
为此,我想从Entity Framework重写DbExpression CommandTrees.我以为这些树是建立跨数据库插入/更新/ select查询.但现在所有相关的构造函数/在命名空间中的工厂新的通用.NET的方式System.Data.Metadata和System.Data.Common.CommandTrees在System.Data.Entity.dll都内!(在msdn中公开记录,如:)DbExpressionBuilder.
有没有人有想法实现这个查询操作有或没有查询树重写?
我想要的代码:( public class DbProviderServicesWrapper : DbProviderServices)
/// <summary>
/// Creates a command definition object for the specified provider manifest and command tree.
/// </summary>
/// <param name="providerManifest">Provider manifest previously retrieved from the store provider.</param>
/// <param name="commandTree">Command tree for the statement.</param>
/// <returns>
/// An exectable command definition object.
/// </returns>
protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, …Run Code Online (Sandbox Code Playgroud) c# expression localization expression-trees entity-framework-4
我读过在需要使用延迟加载和更改跟踪时使用代理.换句话说,要使用延迟加载,我必须启用代理.
到现在为止还挺好.
关键是我可以使用下面的代码设置上下文不使用代理甚至使用延迟加载.
ctx = new SchoolEntities();
ctx.ContextOptions.ProxyCreationEnabled = false;
ctx.ContextOptions.LazyLoadingEnabled = true;
Run Code Online (Sandbox Code Playgroud)
ProxyCreationEnabled属性是仅与更改跟踪代理相关还是我遗漏了某些内容?
有人可以用一些细节解释一下吗?
谢谢!
EDIT1
我没有使用POCO/DbContext.我使用带有ObjectContext的常规edmx EF模型.我知道POCO实体的代理对于更改跟踪和延迟加载的重要性.为何在常规EDMX模型中使用Proxies?
The specified cast from a materialized 'System.Guid' type to the 'System.Int32' type is not valid.
我们有几个WCF服务,其并发模式为Multiple和InstanceContextModeSingle.我们的架构侧重于使用基于构造函数的依赖注入的松散耦合模型.这反过来是使用Unity 2.0实现的(每个服务的web.config都具有在统一容器部分中定义的接口和类型之间的映射).我们的依赖项之一是使用Entity Framework 4与MSSql Server通信的DAL程序集(数据访问层).与数据库通信的类也包含在单位映射中.
当我们运行集成测试时,一切都很棒.但是当我们转移到我们的性能环境来运行负载测试(2,3,4个并发用户)时,我们开始看到以下错误:
System.InvalidOperationException: The 'auth_token' property on 'Session' could not be set to a 'Int32' value. You must set this property to a non-null value of type 'Guid'.
使用以下堆栈:
at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at …Run Code Online (Sandbox Code Playgroud) c# ×7
linq ×2
.net ×1
.net-4.0 ×1
aggregate ×1
asp.net ×1
c#-4.0 ×1
code-first ×1
expression ×1
join ×1
localization ×1
sql ×1
wcf ×1