我如何使用modelBuilder映射这样的东西?其中可以为空的外键引用相同的表主键
Table: Task
taskID int pk
taskName varchar
parentTaskID int (nullable) FK
Run Code Online (Sandbox Code Playgroud)
任务类:
public class Task
{
public int taskID {get;set;}
public string taskName {get;set;}
public int parentTaskID {get;set;}
public Task parentTask {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
...
modelBuilder.Entity<Task>()
.HasOptional(o => o.ParentTask)....
Run Code Online (Sandbox Code Playgroud) entity-framework data-modeling code-first entity-framework-4 entity-framework-ctp5
我正在尝试使用Entity Framework CTP5 Fluent API来映射现有数据库.我有以下课程:
public class Shop
{
public long Id
{
get;
set;
}
}
public class Sale
{
public long Id
{
get;
set;
}
public virtual Shop Shop
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
相应的表称为"商店"和"销售".Sales有一个StoreId外键,指向Stores表中的Id字段.
我正在努力将Sale.Shop.Id映射到表中的StoreId.我无权将其更改为ShopId,因此需要对其进行映射.
在CTP4中,我使用:
modelBuilder.Entity<Sale>().MapSingleType(x =>
new
{
Id = x.Id,
StoreId = x.Shop.Id
});
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
modelBuilder.Entity<Sale>().Property(x => x.Shop.Id).HasColumnName("StoreId");
Run Code Online (Sandbox Code Playgroud)
但是,它似乎只适用于原始类型.
如何指定此映射?
entity-framework fluent-interface foreign-keys map entity-framework-ctp5
为什么我看不到在我的DbContext中添加的存储过程?DbContext由CTP5版本(带有POCO类)引入的模板生成.
我添加了本教程所述的存储过程:http: //thedatafarm.com/blog/data-access/checking-out-one-of-the-new-stored-procedure-features-in-ef4/
此外,我搜索了条目是否在我的上下文中添加,这些是结果:
<Function Name="GetClientsForEmailSend" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" />
<FunctionImport Name="GetClientsForEmailSend" EntitySet="Client" ReturnType="Collection(DBMailMarketingModel.Client)" />
<FunctionImportMapping FunctionImportName="GetClientsForEmailSend" FunctionName="DBMailMarketingModel.Store.GetClientsForEmailSend">
Run Code Online (Sandbox Code Playgroud)
一个类似的问题是:
但我已经完成了所有的建议.
这是存储过程:
ALTER PROCEDURE GetClientsForEmailSend
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT OFF;
-- Insert statements for procedure here
SELECT *
FROM dbo.Client AS c
INNER JOIN Subscription AS i on c.IDClient = i.IDClient
WHERE c.Email is not null and c.Email <> …Run Code Online (Sandbox Code Playgroud) stored-procedures entity-framework entity-framework-ctp5 asp.net-mvc-3
我有一个使用EF CTP5的应用程序.
在这种特殊情况下,我需要降解为一些经典ADO.NET(以读取在存储过程中,其中EF不支持多个结果集).
因此,我试图使用来自对象的现有连接字符串EntityConnection,如下所示:
var ctx = (this as IObjectContextAdapter).ObjectContext;
var efCon = ((EntityConnection) (ctx.Connection)).StoreConnection;
var con = new SqlConnection(efCon.ConnectionString);
con.Open(); // exception thrown
Run Code Online (Sandbox Code Playgroud)
当我调试时,我看到ConnectionString它不包含密码,只包含数据源,用户名,数据库等.
这是一个安全的事情,为什么他们删除它?EF是否在某处隐藏密码并仅在执行存储过程时使用它?
EF连接字符串与经典ADO.NET连接字符串不同,因为它具有元数据信息.
所以看起来我将要删除我需要的连接字符串的一部分,将其放在web.config中并将其传递给存储库.
当然必须有更好的方法!
ado.net connection-string sql-server-2008 entityconnection entity-framework-ctp5
在CTP 4中,我们可以选择我们想要映射的属性,如下所示:
this.MapSingleType(i => new
{
i.Id,
i.OriginalFileName,
i.Extension,
i.MimeType,
i.Width,
i.Height,
i.ImageStoreLocationId,
i.AlternateText,
i.ImageData
});
Run Code Online (Sandbox Code Playgroud)
我们如何在CTP5中实现这一目标?
我尝试使用以下Map配置,但这似乎不起作用,因为我仍然必须显式忽略(this.Ignore(..))我不想映射的属性:
Map(config =>
{
config.Properties(i => new
{
i.OriginalFileName,
i.Extension,
i.MimeType,
i.Width,
i.Height,
i.ImageStoreLocationId,
i.AlternateText,
i.ImageData
});
config.ToTable("Images");
});
Run Code Online (Sandbox Code Playgroud)
考虑到新的API应该更流畅,我必须编写更多代码才能实现同样的功能.
谢谢Ben
当我尝试加载父实体的子实体时,它加载默认值.如果我尝试显式加载它会抛出异常 违反Multiplicity约束."CodeFirstNamespace.Association_Customer"关系的角色"Association_Customer_Target"具有多重性1或0..1.检索复杂图形的子实体时抛出此异常.
我有一个图表关联,它有一个子实体Customer,关系为1到0或1,并且具有独立关联.*主键*是共享的.我正在使用EF6.延迟加载已启用.
public class Association
{
public virtual long Id { get; set; }
public virtual string ExternalId { get; set; }
public virtual int OrganizationId { get; set; }
public virtual AssociationType AssociationType { get; set; }
public virtual Customer Customer {get; set;}
public Association()
{
Customer = new Customer();
}
}
Run Code Online (Sandbox Code Playgroud)
客户类.
public class Customer
{
public virtual long Id { get; set; } …Run Code Online (Sandbox Code Playgroud) entity-framework ef-code-first entity-framework-ctp5 entity-framework-6
我有一个现有的数据库,我想迁移到EF CTP5.我遇到的问题是,当我插入现有数据时,我需要它移动保留当前身份主键以用于其他外键约束.
我想使用EF CTP5代码进行迁移,因此我不必编写大量SQL,甚至不必在SSIS包中包含它.
我试图使用sql语法:
SET IDENTITY_INSERT tableName ON
SET IDENTITY_INSERT tableName OFF
但是,当调用DbContext.SaveChanges()时,上述语法在不同的连接中发生,并且标识列由EF CTP5框架控制,即使在指定ID时也是如此.
是否有一种方法可以禁用或删除与以下类似的对象的身份约定,但是在运行时而不是在实例化数据库时:
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个通用的存储库,我试图添加一个GetById方法,如下所示 C#LINQ to SQL:重构这个通用的GetByID方法
问题是我的存储库不使用System.Data.Linq.DataContext而是使用System.Data.Entity.DbContext
所以我在尝试使用时遇到错误
Mapping.GetMetaType
Run Code Online (Sandbox Code Playgroud)
和
return _set.Where( whereExpression).Single();
Run Code Online (Sandbox Code Playgroud)
如何在CTP5中实现通用的GetById方法?我应该在我的存储库中使用System.Data.Entity.DbContext.
这是我的存储库类的开始
public class BaseRepository<T> where T : class
{
private DbContext _context;
private readonly DbSet<T> _set;
public BaseRepository()
{
_context = new MyDBContext();
_set = _context.Set<T>();
}
Run Code Online (Sandbox Code Playgroud) generics entity-framework repository-pattern entity-framework-ctp5
我有一个PCOO课程
public class Account
{
[Key,DatabaseGenerated(DatabaseGenerationOption.Identity)]
public string AccountId { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
public string Email { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
创建数据库时,我收到以下异常
Identity column 'AccountId' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable.
Run Code Online (Sandbox Code Playgroud)