因为数据库初始化失败,我不得不在不同的MVC 4代码第一技术教程之后停止在同一阶段.
使用连接
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-DbTestApp-20130205173443;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-DbTestApp-20130205173443.mdf" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)
我甚至无法创建或管理数据库,我希望从我的模型生成数据库
public class Category
{
public int Id { get; set; }
[Required]
[MinLength(4)]
[MaxLength(64)]
public string Name { get; set; }
public virtual IEnumerable<Article> Articles { get; set; }
}
public class Article
{
public int Id { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
[Required]
[MinLength(4)]
[MaxLength(64)]
public string Title { get; set; }
[Required]
[MinLength(16)] …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加到MVC 4开箱即用的会员资格.
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, model.Mobile);
//I added model.Mobile
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我改变了模型
public class RegisterModel
{
// Username and password methods
public string Mobile { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我还将它添加到UserProfile模型中.我收到一个SqlException,我只是没有看到连接.这似乎是一个简单的错误但是.设置DB Nvarchar(MAX)以尝试避免此错误.
Invalid column name 'Length'
Run Code Online (Sandbox Code Playgroud) 我正在用NUnit和Entity Framework编写一些UnitTests.如何从Entity Framework级别删除整个localdb数据库?
注意:我不想清除表的数据.我想删除整个数据库.
此外,我可以在我的应用程序工作目录中创建一个localdb文件,前提是尚未创建数据库:
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "");
var testDbFileName = String.Format(@"UnitTestDB.mdf");
var testDbFileNameWithPath = path + @"\" + testDbFileName;
var connectionString =
String.Format(
@"Data Source=(localdb)\V11.0;Initial Catalog={0};Integrated Security=True;AttachDBFilename={1};MultipleActiveResultSets=True",
"UnitTestDB", testDbFileNameWithPath);
//Here would be the code passing connection string to DbContext and so on..
Run Code Online (Sandbox Code Playgroud)
仅删除文件"UnitTestDB.mdf"是不够的.在SQL Management Studio中仍然有对db的引用
public virtual ICollection<Product> Products { get; set; }POCO课程的目的是什么?
public class Category
{
[ScaffoldColumn(false)]
public int CategoryID { get; set; }
[Required, StringLength(100), Display(Name = "Name")]
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我有以下课程(过于简化):
public class Person
{
public int ID { get; set; }
}
public class Content
{
public int ID { get; set; }
}
public class Image : Content
{
public bool Private { get; set; }
public Person Author { get; set; }
}
public class Tag
{
public int ID { get; set; }
public Content Content { get; set; }
public Person Person { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想获得所有Tags在Content是Image与Image …
我试图弄清楚如何使MVC脚手架使用复合/复合键.
我有下表:
public class Data
{
[Key, Column(Order = 0)]
[ForeignKey("Note")]
[Display(Name = "Note id")]
public int NoteId { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Member")]
[Display(Name = "Member id")]
public int MemberId { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
[Display(Name = "Note")]
public virtual Note Note { get; set; }
[Display(Name = "Member")]
public virtual Member Member { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我执行脚手架的行时:
Scaffold Controller Data -Repository
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Get-PrimaryKey : Cannot …Run Code Online (Sandbox Code Playgroud) asp.net-mvc entity-framework scaffolding composite-key ef-code-first
我有一种奇怪的行为.我想更新复杂类型的单个属性.当我指定要使用IsModified更新的属性(某些属性为true而某些属性为false)时,我没有更新任何内容.如果我没有指定复杂类型的属性,则更新复杂属性的每个字段.
public class MyEntity
{
public MyComplexClass Property1 { get; set; }
}
//... The code below doesn't work, in fact it update nothing
var entityLocal = this.Set<MyEntity>().Local.SingleOrDefault(d => d.Id == entity.Id);
if (entityLocal == null)
{
entityLocal = this.Set<MyEntity>().Attach(entity);
}
this.ChangeTracker.Entries<MyEntity>().Single(d => d.Entity == entityLocal).State = EntityState.Modified;
this.Entry(entity).Property(s => s.Property1.SubProperty1).IsModified = true;
this.Entry(entity).Property(s => s.Property1.SubProperty2).IsModified = false;//This seam to remove all update of the complex type...?
this.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
这产生:
update [dbo].[MyEntity]
set @p = 0
where (([Id] = @0))
Run Code Online (Sandbox Code Playgroud)
如果我没有将SubMperty2的IsModified指定为false,我在SQL事件探查器中有以下内容: …
c# entity-framework complextype ef-code-first entity-framework-5
这是基于Programming Code First EF的示例.
请看下面的课程.当PersonRepository调用实例化时,调用InsertOrUpdate方法返回Null值.据我所知,IQuariable的FitstOrDefault不应该返回Null.
这里有什么问题.谢谢您的帮助
public class Person
{
public int PersonId { get; set; }
public int SocialSecurityNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//public int MyProperty { get; set; }
public List<Lodging> PrimaryContactFor { get; set; }
public List<Lodging> SecondaryContactFor { get; set; }
public PersonPhoto Photo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
照片类
public class PersonPhoto
{
public int PersonID { get; set; }
public …Run Code Online (Sandbox Code Playgroud) .net entity-framework code-first ef-code-first entity-framework-5
考虑一个包含三个行的名称的数据库表:
SubjectID StudentName
--------- -------------
1 Peter
2 Paul
2 Mary
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以将其转换为实体框架中的单个字符串?这样的事情:
SubjectID StudentName
---------- -------------
1 Peter
2 Paul, Mary
Run Code Online (Sandbox Code Playgroud)
查看此链接以获取更多信息.
我有以下实体:
public class Module
{
public Module()
{
SubModules = new List<Module>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Action { get; set; }
public string Controller { get; set; }
public string Icon { get; set; }
public List<Module> SubModules { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
其中,当通过Code First初始化时,生成下表Schema:
CREATE TABLE [dbo].[Modules](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](max) NULL,
[Action] [nvarchar](max) NULL,
[Controller] [nvarchar](max) NULL,
[Icon] [nvarchar](max) NULL,
[Module_Id] [int] …Run Code Online (Sandbox Code Playgroud) ef-code-first ×10
c# ×5
code-first ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
complextype ×1
inheritance ×1
linq ×1
localdb ×1
scaffolding ×1