在Entity Framework 4中,有"从数据库更新模型"和"从模型生成数据库"选项.但我缺少的是一个选项
从模型更新数据库
它通过修改数据库模式(例如添加新列)反映模型中所做的更改(例如,添加新的Property或Navigation-Property).不失其内容.
有人知道实现这一目标的方法,还是有一个t4模板可以在不删除现有表的情况下执行模式更新?(我使用的是Visual Studio 2010,.Net 4.0和SQL Server 2008)
谢谢
我有以下设置(工作正常).使用CodeFirst(CTP4).
模板具有影响列表,每个影响赋予特征值.
public class Template
{
public virtual int Id { get; set; }
public virtual ICollection<Influence> Influences { get; set; }
}
public class Influence
{
public virtual int Id { get; set; }
public virtual Trait Trait { get; set; }
public virtual int Value { get; set; }
}
public class Trait
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
模板配置如下.
public class TemplateConfiguration : EntityConfiguration<Template>
{ …Run Code Online (Sandbox Code Playgroud) 我在VS2010,POCO和模型优先方法中使用EF4.
我的实体具有以下属性:Id:Guid,Name:String,Created:DateTime,Modified:DateTime,Revision:Int32.
我创建我的实体,设置名称并使用EF4上下文将其保存到数据库.这应该将Id设置为新的Guid(与Identity-SGP一起使用),Created设置为now,Modified left as null,Revision设置为0.我检索实体,更改名称并再次保存.这次Modified-value应设置为now,revision应为1.
如何使用EF4与EDMX设计师最好地完成此操作?
更新:
这是我最终使用的:
public override int SaveChanges(System.Data.Objects.SaveOptions options)
{
foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified).Where(e => e.Entity is EntityBase))
{
EntityBase entity = entry.Entity as EntityBase;
if (entry.State == EntityState.Added)
{
entity.Version = new Version() { Major = 1, Minor = 0, Revision = 0 };
entity.Created = DateTime.Now;
if (OperationContext.Current != null) entity.CreatedBy = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
}
else if (entry.State == EntityState.Modified)
{
entity.Version.Revision++;
entity.Modified = DateTime.Now;
if (OperationContext.Current != null) entity.ModifiedBy = …Run Code Online (Sandbox Code Playgroud) 我是WPF和EF的新手,我正在尝试从数据网格中的表中显示一些数据.我已经从现有数据库中提取了实体模型,并且简单的操作似乎正常工作(使用'first'获取行数).
我使用2.0.5 DDEX提供程序和2.5.2 ADO NETProvider运行Firebird 2.5.0.
当我尝试将数据放入网格或简单地进入列表时,我得到一个空引用异常.
可能我只是不明白如何使用实体框架,但我在网上看到的例子让它看起来非常简单.
public partial class Page1 : Page
{
Entities context;
public Page1()
{
context = new Entities();
InitializeComponent();
// This works to get a row into the grid
var arep = context.SALESREPs.First();
var alist = new List<SALESREP>();
alist.Add( arep );
gridUserList.ItemsSource = alist;
// These both fail with null ref exception
var allreps = context.SALESREPs.ToList();
gridUserList.ItemsSource = context.SALESREPs;
}
}
Run Code Online (Sandbox Code Playgroud)
这是例外细节:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an …Run Code Online (Sandbox Code Playgroud) 使用Entity Framework 4时,如何从存储过程创建单个实体?
我们有一个ASP.Net MVC应用程序,它使用EF4作为其数据访问层,我们看到当我们认为它们应该是没有抛出OptimisitcConcurrencyExceptions时的意外行为.
我们已将问题简化为以下代码...
using System.Linq;
using Project.Model;
namespace OptimisticConcurrency
{
class Program
{
static void Main()
{
Contact firstContact = null;
using (var firstEntities = new ProjectEntities())
{
firstContact = (from c in firstEntities.Contacts
where c.LastName == "smith" select c).Single();
}
using (var secondEntities = new ProjectEntities())
{
var secondContact = (from c in secondEntities.Contacts
where c.LastName == "smith" select c).Single();
secondContact.Title = "a";
secondEntities.SaveChanges();
}
firstContact.Title = "b";
using (var thirdEntities = new ProjectEntities())
{
var thirdContact = (from …Run Code Online (Sandbox Code Playgroud) 我是一名NHibernate开发人员,试图在一个爱好项目中为Entity Framework提供一个机会.我习惯使用Fluent NHibernate在代码中指定映射数据.赦免微软认为不应允许开发人员编写代码的信念,我试图使用Entity Framework的可视化设计器表面(通过在Visual Studio中打开.edmx文件获得)来创建映射.
我不知道如何建立多对多的关系!我已经从数据库"更新了模型",但是我得到了两个一对多的关系,这个关系是一个对应于联结表的新实体(它只包含外键和它自己的主键).
到目前为止,所有尝试通过点击实体和关系来解决这个问题都失败了.任何人都可以给我指针吗?
我是.NET实体框架的新手,我正在尝试迭代数百万行的表中的记录.这是基本代码:
// select the records from the database
var records = from data in dataContext.Messages
select data;
// iterate over the messages
foreach (var record in records)
{
// do nothing
}
Run Code Online (Sandbox Code Playgroud)
在迭代数据时,我得到了"outofmemoryexception".有什么方法可以改变我的查询或管理ObjectQuery实例的内存?
我有以下POCO类及其存储库模式实现.如果我的模型足够大,那么制作这个通用是有意义的,因此只需要完成一个实现.
这可能吗?你能告诉我怎么样吗?
public class Position
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int PositionID { get; set; }
[StringLength(20, MinimumLength=3)]
public string name { get; set; }
public int yearsExperienceRequired { get; set; }
public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
}
public interface IPositionRepository
{
void CreateNewPosition(Position contactToCreate);
void DeletePosition(int id);
Position GetPositionByID(int id);
IEnumerable<Position> GetAllPositions();
int SaveChanges();
IEnumerable<Position> GetPositionByCustomExpression(Expression<Func<Position, bool>> predicate);
}
public class PositionRepository : IPositionRepository
{
private HRContext _db = new HRContext();
public PositionRepository(HRContext context)
{
if (context == …Run Code Online (Sandbox Code Playgroud) 我有一个包含序号的表(想想发票号或学生证).
在某些时候,用户需要请求前一个号码(以便计算下一个号码).一旦用户知道当前号码,他们就需要生成下一个号码并将其添加到表格中.
我担心的是,由于并发访问,两个用户将能够错误地生成两个相同的号码.
我听说过存储过程,我知道这可能是一个解决方案.这里有最佳实践,以避免并发问题吗?
编辑:这是我到目前为止所拥有的:
USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetNextOrderNumber]
AS
BEGIN
BEGIN TRAN
DECLARE @recentYear INT
DECLARE @recentMonth INT
DECLARE @recentSequenceNum INT
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- get the most recent numbers
SELECT @recentYear = Year, @recentMonth = Month, @recentSequenceNum = OrderSequenceNumber
FROM dbo.OrderNumbers
WITH (XLOCK)
WHERE Id = (SELECT MAX(Id) FROM …Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×3
sql-server ×2
ado.net ×1
concurrency ×1
firebird ×1
firebird2.5 ×1
many-to-many ×1