我有一个启用并配置为使用我的数据库和一些额外的表/字段的ASP.NET-MVC应用程序.MigrationSimpleMemberShipProvider
我的问题是,我的数据库初始化在哪里?因为我还在开发它,我可以删除数据库/表和数据并重新创建它.
我已将它放在configuration.cs文件中,但我不确定这是否是数据库初始化程序的位置.
namespace CFContext.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using WebMatrix.WebData;
using System.Web.Security;
using System.Collections.Generic;
internal sealed class Configuration : DbMigrationsConfiguration<DataContext>
{
public Configuration()
{
Database.SetInitializer(new DropCreateDatabaseAlways<DataContext>());
//Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataContext>());
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(DataContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g. …Run Code Online (Sandbox Code Playgroud) 在SQL Server 2008中,我有一个这样的视图:
CREATE VIEW products AS
SELECT a.ID, a.Name, b.ID as SubID, b.Name as SubName
FROM main_product as a
INNER JOIN sub_product as b on a.ID = b.mainID
Run Code Online (Sandbox Code Playgroud)
这是我的模特:
Public class Products
{
public int ID { get; set; }
public int Name { get; set; }
public int SubID { get; set; }
public int SubName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我如何将MVC3的视图映射到SQL Server视图?
UPDATE
我试过这个:
public class PartialContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); …Run Code Online (Sandbox Code Playgroud) sql-server entity-framework sql-view ef-code-first ef-database-first
我不是一个经验丰富的MVC3开发人员,但我想成为.我熟悉POCO类和ViewModels,因为前者描述了数据库的每个类,后者用于mvc3中的强类型视图.对于经验丰富的开发人员来说,我的问题并不复杂,但我对此感到有些困惑.
问题是,我有一个包含三个项目的解决方案;
.
public class Service
{
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int ServiceID { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(30)]
[LocalizedAttribute("Name")]
public string Name { get; set; }
//------------------------------------------------------------//
[MaxLength(100)]
[LocalizedAttribute("Description")]
public string Description { get; set; }
//------------------------------------------------------------//
[Required]
public long ModifiedByUserID { get; set; }
[ForeignKey("ModifiedByUserID")]
public virtual User OperatorUser { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(10)]
public int ModifiedDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
存储库和UnitOf Work类库
MVC应用程序
现在,我是否正确地解决了POCO课程?(我当然使用EF Code First生成数据库)如果是,那么它们也被推断为ViewModels吗?我用它们来生成强类型视图.
定义POCO类和ViewModel的最佳和实际标准方法是什么?
我很感激任何善意的指导,
我有两个简单的类,User并且Task:
class User
{
public int UserId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
class Task
{
public int TaskId { get; set; }
public string Name { get; set; }
public DateTime CreatedAt { get; set; }
public virtual ICollection<User> Followers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Task类有一个属性Followers是一个ICollection<User>
这是db上下文类:
class MyContext : DbContext
{
public DbSet<User> Users { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有一个Business和一个Category模型.
每个Business都有很多Categories通过暴露的集合(Category无视Business实体).
现在这是我的控制器动作:
[HttpPost]
[ValidateAntiForgeryToken]
private ActionResult Save(Business business)
{
//Context is a lazy-loaded property that returns a reference to the DbContext
//It's disposal is taken care of at the controller's Dispose override.
foreach (var category in business.Categories)
Context.Categories.Attach(category);
if (business.BusinessId > 0)
Context.Businesses.Attach(business);
else
Context.Businesses.Add(business);
Context.SaveChanges();
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
现在有几个business.Categories已经CategoryId设置为现有Category(缺少tho 的Title属性Category).
从服务器点击SaveChanges并重新加载后Business,Categories …
在EF Code First中,我们可以通过这样的编码来创建一对一的关系:
public class User
{
public int UserID {get;set;}
public string Name {get;set;}
public int UserDetailID {get;set;}
public UserDetail Detail {get;set;}
}
public class UserDetail
{
public int UserDetailID {get;set;}
public string Address {get;set:}
public int UserID {get;set;}
public User User {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在Visual Studio 2012中首先使用EF数据库创建相同的关系时,我遇到了麻烦.这是我的代码:
CREATE TABLE [dbo].[Users] (
[UserID] UNIQUEIDENTIFIER CONSTRAINT [DF_Users_UserID] DEFAULT (newid()) NOT NULL,
[UserDetailID] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED …Run Code Online (Sandbox Code Playgroud) 您好我正在尝试添加一个可以将文件存储到现有ASP.NET MVC Code First项目的模型.
namespace MyMVCApp.Models
{
public class FileUpload
{
[Key]
public int ID { get; set; }
public string FileName { get; set; }
public string FilePath { get; set; }
[Required]
public HttpPostedFileBase File { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我已将以下内容添加到项目的DbContext文件中:
public DbSet<FileUpload> FileUploads { get; set; }
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用Package Manager控制台中的Entity-Framework Migrations将表添加到现有数据库:
PM> Add-Migration FileUpload
Run Code Online (Sandbox Code Playgroud)
在运行ubove命令时,我收到以下错误:
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'HttpPostedFileBase' has no key defined. Define …Run Code Online (Sandbox Code Playgroud) 我和Apple桌子之间有很多关系.
public class Apple
{
[Key]
public int AppleID { get; set; }
[Required]
public string AppleName { get; set; }
[NotMapped]
public int[] SelectedOranges { get; set; }
public virtual ICollection<Orange> Oranges { get; set; }
}
public class Orange
{
[Key]
public int OrangeID { get; set; }
public String OrangeName { get; set; }
public virtual ICollection<Apple> Apples { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在Data Context类中,OnModelCreating方法被覆盖,如下所示
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//----------------------------------------------------------------
//Creating a Association (Intermediate …Run Code Online (Sandbox Code Playgroud) 我们将使用MVC 4和EF 5为私营公司开发一个中大型定制Web应用程序.
早期分析和以前在该业务领域的经验表明,它将拥有超过150个表/实体,因为我们的客户不是软件工程师,我们知道我们的数据模型将在项目进展中多次改变.
现在,根据以下内容,我的Q是哪种方法对我们更好:
1)由于会发生许多变化,因此更新数据模型和数据存储的工作量减少.
2)创建数据存储所需的时间更少,并有助于更快地推进项目.
注意:此应用程序将使用大量数据(对于某些实体,10k,100k对象).但是,它将很少(有时可能没有)并发请求和在线用户.
提前致谢
database-design ef-code-first ef-database-first asp.net-mvc-4 entity-framework-5
我在更新DB中的现有实体时遇到问题.我正在使用Entity Framework Code First.
我有计算食物的小程序.
我的域类>
public class Unit
{
public int Id { get; set; }
public string Name { get; set; }
}
public abstract class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Ingredient : Item
{
public virtual Unit Unit { get; set; }
public decimal Price { get; set; }
}
public class Recipe : Item
{
public virtual List<RecipeItem> Items { get; set; …Run Code Online (Sandbox Code Playgroud) ef-code-first ×10
c# ×4
.net ×2
asp.net-mvc ×1
code-first ×1
database ×1
foreign-keys ×1
many-to-many ×1
one-to-one ×1
poco ×1
sql-server ×1
sql-view ×1