我遇到了一个令人困惑的问题,在我的编辑或创建操作结果方法中,EF4将抛出一个DbEntityValidationException,内部消息说明:
字段Body必须是字符串或数组类型,最大长度为"128".
有问题的模型如下所示:
[Table("tblArticles")]
public class Article
{
[Key]
public int ID { get; set; }
[Required(ErrorMessage="Title must be included")]
public string Title { get; set; }
[AllowHtml]
public string Body { get; set; }
[Required(ErrorMessage="Start Date must be specified")]
[Display(Name="Start Date")]
[DisplayFormat(DataFormatString="dd-mm-yyyy")]
public DateTime? StartDate { get; set; }
[Required(ErrorMessage = "End Date must be specified")]
[Display(Name = "End Date")]
public DateTime? EndDate { get; set; }
public int Priority { get; set; }
public bool Archived { get; …
Run Code Online (Sandbox Code Playgroud) 我有一个实体类
public class Employee
{
public long Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已将Id字段设置为具有自动编号生成的主键
modelBuilder.Entity<Employee>().HasKey(e => e.Id);
modelBuilder.Entity<Employee>().Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Run Code Online (Sandbox Code Playgroud)
但我希望Identity从10000而不是从1开始播种,这是默认值.如何在EF中指定?
如何通过ID获取另一个列表中的所有元素?例如; 我有List角色; 我想通过他们的Id获取此列表中数据库中的所有角色.
我正在使用代码优先.
我做了这个,它扔了一个错误:
var roles = db.Roles.Where(r => user.Roles.Any(ur => ur.RoleId == r.RoleId));
Run Code Online (Sandbox Code Playgroud)
RoleId
是int类型.
错误:
无法创建"SampleMVC.Domain.Role"类型的常量值.在此上下文中仅支持原始类型(例如Int32,String和Guid').
我当前的项目基于Entity Framwork代码优先.我有三种类型:Task,TaskType和Module.
public class Task
{
public int ID { get; set; }
public Module Module { get; set; }
public TaskType Type { get; set; }
}
public class TaskType
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Module
{
public int ID { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在表内为Task-type定义了外键关系.
我的问题是,当我尝试创建一个链接到已经可用的TaskType和Module对象(ID = 1)的新Task对象时,这些对象将在其相应的表中创建为新行.
TaskRepository repo = new TaskRepository();
Task task = new Task(); …
Run Code Online (Sandbox Code Playgroud) 我正在使用Entity Framework 4.1(代码优先)开展一个小型示例项目.我的课程看起来像这样:
public class Context : DbContext
{
public IDbSet<Person> People { get; set; }
public IDbSet<EmployeeType> EmployeeTypes { get; set; }
}
public class Person
{
[Key]
public int Key { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
virtual public EmployeeType EmployeeType { get; set; }
}
public class EmployeeType
{
[Key]
public int Key { get; set; }
public string Text { get; set; }
virtual public …
Run Code Online (Sandbox Code Playgroud) 您好我发现了一个带有WEBINVOKE方法的wcf REST示例,如下所示
[OperationContract]
[WebInvoke(
BodyStyle=WebMessageBodyStyle.Bare,
Method="POST",
RequestFormat=WebMessageFormat.Xml,
ResponseFormat=WebMessageFormat.Xml,
UriTemplate="CreateStudent/{StudentName}/{Chair}/{AverageNote}")]
int Insert(string StudentName, string Chair, string AverageNote);
[OperationContract]
[WebGet(
BodyStyle= WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
Student[] GetAllStudents();
Run Code Online (Sandbox Code Playgroud)
我的问题是,我可以使用WEBGET方法而不是WEBINVOKE,就像下面的WEBINVOKE POST和WEBGET之间的差别一样,根据我的观察,我们通过在WEAPGet和WebInvoke POST的URI模板中附加查询字符串来发送参数,使用WEBGET我们无法获得的WebInvoke POST有什么优势
[OperationContract]
[WebGet(
BodyStyle=WebMessageBodyStyle.Bare,
RequestFormat=WebMessageFormat.Xml,
ResponseFormat=WebMessageFormat.Xml,
UriTemplate="CreateStudent/{StudentName}/{Chair}/{AverageNote}")]
int Insert(string StudentName, string Chair, string AverageNote);
Run Code Online (Sandbox Code Playgroud) 我有以下型号:
class Contract
{
string ContractID{get;set;}
ICollection<Part> Parts{get;set;}
}
class Part
{
string PartID{get;set;}
ICollection<Contract> Contracts{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
问题是Part和Contract之间的关系还包含以下附加信息:
class ContractParts
{
Contract{get;set;}
Part{get;set;}
Date{get;set;} //additional info
Price{get;set;} //additional info
}
Run Code Online (Sandbox Code Playgroud)
我将如何为此编写实体上下文?
我正在尝试实体框架代码优先.我似乎无法RecreateDatabaseIfModelChanges
在WPF 4.0中找到要使用的程序集/命名空间.这是仅限ASP.NET的功能吗?如果没有,我应该参考哪个装配?
这是我的代码:
using System;
using System.Data.Entity;
using System.Windows;
using CodeFirstTester.Models;
namespace CodeFirstTester
{
public partial class App : Application
{
static App()
{
// this fails:
Database.SetInitializer(new RecreateDatabaseIfModelChanges<NerdDinners>());
// The type or namespace name 'RecreateDatabaseIfModelChanges'
// could not be found (are you missing a using directive or
// an assembly reference?)
using (var nerdDinners = new NerdDinners())
{
var dinner = new Dinner()
{
Title = "Party at Scott's House",
EventDate = DateTime.Parse("12/31/2010"),
Address = "Building 40", …
Run Code Online (Sandbox Code Playgroud) 我正在使用实体框架4,在表中使用实体框架插入新记录而不是插入触发器而表有一个标识列时,而不是触发器用于根据某些逻辑修改插入值之一,实体框架引发异常"存储更新,插入或删除语句影响了意外的行数(0).实体可能已被修改或删除,因为实体已加载.刷新ObjectStateManager条目".
任何人都可以帮助解决这个异常吗?
嗨,我打算在我的一个项目中测试EF Code First.这就是我想要的.我有三张桌子,结构如下
public partial class App_user
{
public int id { get; set; }
public string name { get; set; }
public string email_address { get; set; }
public string password { get; set; }
public int user_type { get; set; }
public List<Role> Roles { get; set; }
}
public partial class Role
{
public int id { get; set; }
public string name { get; set; }
}
public partial class User_role
{
public int user_id { …
Run Code Online (Sandbox Code Playgroud)