我想在模型上使用延迟加载集合,但我希望通过单独的方法完成添加/删除功能.所以像这样:
class Model
{
protected virtual ICollection<Something> _somethings { get; set; }
public IEnumerable<Something> Somethings
{
get { return _somethings; }
}
public void AddSomething(Something thingToAdd)
{
/* logic */
_somethings.Add(thingToAdd);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何为此配置映射.我研究了使用配置类:EntityConfiguration.但由于该属性受到保护,我无法弄清楚如何在其上设置配置.有没有办法完成我在这里要做的事情?
所以说我有2个实体,Post并且PostHistory.每当我创建或编辑帖子时,我都想创建一个精确的副本,PostHistory以记录对帖子所做的所有更改.假设post实体具有以下定义:
public class Post
{
public int Id { get; set; }
public string Text { get; set; }
public virtual ICollection<PostHistory> PostHistory{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我创建第一个问题时,问题就来了Post.例如,如果我试试这个:
Post post = new Post { Text = "Blah" };
context.Posts.Add(post);
PostHistory history = new PostHistory { Text = "Blah"};
context.PostHistory.Add(history);
post.PostHistory.Add(history);
Run Code Online (Sandbox Code Playgroud)
这将失败,因为因为这两个post和history是新的实体,Post.PostHistory因为它没有尚未初始化为空.我能看到这样做的唯一方法是首先提交postdb,然后提交history到数据库,但我不明白为什么要为此执行2个单独的插入.
如果我有一个构造函数初始化ICollection到List<T>一切正常,但强制实现List<T>导致其他问题,几乎没有代码优先教程这样做强制启动.
那么处理这种情况的最佳方法是什么?
c# entity-relationship entity-framework code-first entity-framework-4
我首先使用EF代码,我需要在一个表中插入许多行,几千条记录,我在一个通用列表中.只是做一个foreach循环并将每个实体添加到表中并使用SaveChanges()完成需要相当长的时间.
有没有一种笨重的方式首先使用EF或代码执行此操作,还是应该执行SqlCommand或SqlBulkCopy?另外,我是否可以先在EF代码中关闭以使插入更快?
谢谢您的帮助!
我正在尝试如何为ASP.NET MVC3配置StructureMap我已经使用NuGet,我注意到它创建了一个名为StructuremapMVC的cs文件的App_Start文件夹,所以我检查它并注意到它是相同的代码但是简化了将在Global.asax上的App_Start部分手动编写...
这是我在IoC Class中的代码
public static class IoC
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IController>();
});
x.For<OpcionDB>().Use(() => new DatabaseFactory().Get());
});
return ObjectFactory.Container;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么当我在控制器上注入一些IoC时会出现异常(我使用这种模式:实体框架4 CTP 4/CTP 5通用存储库模式和单元可测试):
private readonly IAsambleaRepository _aRep;
private readonly IUnitOfWork _uOw;
public AsambleaController(IAsambleaRepository aRep, IUnitOfWork uOw)
{
_aRep = aRep;
this._uOw = uOw;
}
public ActionResult List(string period)
{
var rs = _aRep.ByPeriodo(period).ToList<Asamblea>();
return View();
}
Run Code Online (Sandbox Code Playgroud)
例外显示:
Exception Details: System.MissingMethodException: No parameterless constructor defined …Run Code Online (Sandbox Code Playgroud) structuremap asp.net-mvc entity-framework code-first asp.net-mvc-3
这就是事情.
我有一个接口,我想把Include属于EntityFramework库的扩展方法放到我的IRepository层,不需要知道EntityFramework.
public interface IRepository<TEntity>
{
IQueryable<TEntity> Entities { get; }
TEntity GetById(long id);
TEntity Insert(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
void Delete(long id);
}
Run Code Online (Sandbox Code Playgroud)
所以我有扩展方法:
public static class IncludeExtension
{
static IQueryable<TEntity> Include<TEntity>(this IQueryable<TEntity> query,
string path)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在这一层实现它,我想把它发送到我的EntityFramework(或任何将实现IRepository的人)来处理.
我需要一个带扩展方法的接口.
有光吗?
似乎Code First DbContext在编译期间真的使用了给定的ConnectionString吗?我甚至不知道这是怎么可能的,但对我来说似乎是这样.如果我关闭我的本地SQL Server,我收到错误,指出"无法获取DbContext类型的MetadataWorkspace ...".打开SQL Server,一切都编译好了.
这是我的上下文的一部分(我使用的是现有的数据库,是的,我知道,实际上并不是第一个代码)
public class MyContext : DbContext
{
public MyContext() : base("MY_DYNAMIC_CONNECTIONSTRING")
{
Database.SetInitializer<MyContext>(null);
}
...
Run Code Online (Sandbox Code Playgroud)
如果确实如此,那就是一个很大的问题.我怎么能阻止它呢?如果我使用ConnectionString不起作用的单独构建机器怎么办?或者我做错了什么?有什么建议?
entity-framework connection-string code-first wcf-ria-services ef-code-first
我如何在ServiceStack中使用asp.net成员资格?(ServiceStack.OrmLite,ServiceStack.Host.AspNet等)
membership asp.net asp.net-membership code-first servicestack
我陷入了这个错误,无法找到解决方案.我尝试了几件事,但无法提出解决方案.
这是我的问题:
码:
namespace ProjectTracker.Database.Entities
{
[DataContract]
public class User
{
[DataMember]
public int Id { get; set; }
[Required]
[MaxLength(50)]
[DataMember]
public string UserName { get; set; }
[Required]
[MaxLength(100)]
[DataType(DataType.Password)]
[DataMember]
public string Password { get; set; }
[DataMember]
public bool IsPasswordExpired { get; set; }
[Required]
[DataMember]
public DateTime CreatedDate { get; set; }
[Required]
[ForeignKey("CreatedBy")]
[DataMember]
public int CreatedByUserId { get; set; }
[DataMember]
public virtual User CreatedBy { get; set; }
[Required]
[DataMember]
public DateTime …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以从一个已定义的实体对象类创建两个表实例.
例:
public class EntityA()
{
public String name {get; set;}
public String value {get; set;}
}
public class MyDbConext : DbContext
{
public DbSet<EntityA> instance1{ get; set; }
public DbSet<EntityA> instance2{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我要做的是创建两个具有不同表名的实体A实例.这可能与代码第一实体框架?我觉得必须创建另一个扩展实体EntityA的类来创建同一个实体的另一个实例似乎很乏味.
期望的输出:
DBConext中的代码将引发异常.
任何建议表示赞赏,谢谢,D
我是实体框架的新手.我写了一个简单的代码第一个数据库 如何按类更改database.mdf的物理位置.让我说我要在| DataDirectory |\App_Data\database.mdf中保存我的数据库.我正在使用Windows窗体.这是DbContext
class DatabaseContext : DbContext
{
public DatabaseContext()
: base("database")
{ }
public DbSet<Login> Logins { get; set; }
public DbSet<master> masters { get; set; }
public DbSet<People> People { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是配置文件app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory> …Run Code Online (Sandbox Code Playgroud) code-first ×10
c# ×4
.net ×2
asp.net ×2
.net-4.5 ×1
asp.net-mvc ×1
membership ×1
methods ×1
servicestack ×1
structuremap ×1