代码改变世界

Some notes in learning MVC and EF

2012-04-03 19:20  Wizardlsw  阅读(522)  评论(0编辑  收藏  举报

            在StackOverflow上的问题及答案:http://stackoverflow.com/questions/5040795/adding-index-to-a-table/5041221

            示例代码如下:

var query = from p in context.Persons 
            where p.Name.Contains("abc") 
            select p; 

...

public class MyInitializer : CreateDatabaseIfNotExists<MyContext> 
{ 
  protected override void Seed(MyContext context) 
  { 
    context.Database.SqlCommand("CREATE INDEX IX_Person_Name ON Person (Name)"); 
  } 
} 
  • Some commands while using Entity Framework 4.3.
    • Clear
    • Enable-Migrations  [-ContextTypeName Mvc4App.Models.UsersContext]
    • Add-Migrations InitialCreate
    • Update-Database –?
    • Add-Migration AddProductPrice      
      • here “AddProductPrice” is arbitrary for the migration
    • Update-Database [-Verbose] 
      • apply the changes
    • Update-Database  -TargetMigration : “InitialCreate” [-Force]
    • Update-Database –Script
    • Update-Database –Script –SourceMigrations|$InitialDatabase –TargetMigration|”AddProductPrice”
      • or rollback to an empty database: Update-Database –TargetMigration:$InitialDatabase

             Reference:

        解决方法:在数据升级代码加上try{}catch(DbEntityValidationException ex){} 进一步debug一下查看DbEntityValidationException详情。

            try
            {
                var migrator = new DbMigrator(new Configuration());
                migrator.Update();
            }
            catch (DbEntityValidationException ex1)
            {
                //CHEKC ex1.EntityValidationErrors here
            }
            catch (Exception ex2)
            {
                Debug.WriteLine(ex2.Message);
            }

 

 

  • The name ‘ViewBag’ does not exist in the current context.

             Answer: You need to add the MVC-specific Razor configuration to your web.config

 

  • Hot to imports namespace in Razor view?

For C#: @using MyNamespace

For VB.NET: @Imports MyNamespace

另一种做法是找views文件夹下面的web.config 文件,修改namespaces节点。

<system.web.webPages.razor> 
  <pages pageBaseType="System.Web.Mvc.WebViewPage"> 
    <namespaces> 
      <add namespace="System.Web.Mvc" /> 
      <add namespace="System.Web.Mvc.Ajax" /> 
      . 
      . 
      <!-- etc --> 
    </namespaces> 
  </pages> 
</system.web.webPages.razor> 

reference: http://stackoverflow.com/questions/3239006/how-to-import-a-namespace-in-razor-view-page

Questions And Answers for MVC

  • How to use AreaRegistration

              选中Web项目,右击 "add”, 选择Area,在弹出的窗口中输入想添加的Area名称。

  • How to specify a different master layout
  • How to specify the data column’s type in database, like NText?
  • How to create custom  HTML helper?
  • How to display a navigation property witch is type of ICollection<T> type?
  • Introducing System.Web.Providers – ASP.NET Universal Providers for Session, Membership, Roles and User Profile on SQ Compact and SQL Azure

Reference: http://www.hanselman.com/blog/IntroducingSystemWebProvidersASPNETUniversalProvidersForSessionMembershipRolesAndUserProfileOnSQLCompactAndSQLAzure.aspx

  • 对于在实体类中枚举类型的属性如何处理,目前EF不会为枚举类型的字段创建相应的表字段。
        private int _columnTypeInt = (int)SiteColumnType.CommonContent;
        [Required]
        public int ColumnTypeInt { 
            get { return _columnTypeInt; } 
            set { _columnTypeInt = value; }
        }

        public SiteColumnType ColumnType
        {
            get
            {
                return (SiteColumnType)ColumnTypeInt;
            }
            set {
                ColumnTypeInt = (int)value;
            }
        }

          

  • 如何进行实体拆分:

            Reference:

            http://msdn.microsoft.com/zh-cn/magazine/hh126815.aspx

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Post>()
            .Map(m =>
            {
                m.Properties(p => new { p.Title, p.Content });
                m.ToTable("Posts");
            })
            .Map(m =>
            {
                m.Properties(p => new { p.Photo });
                m.ToTable("PostPhotos");
            });
        }
  • SelectList:
http://www.itstrike.cn/Question/b7cb01cb-67f5-4379-84d3-c9cfaaad2304


Trouble fixings:
- Using LocalDB with Full IIS, Part 1: User Profile http://blogs.msdn.com/b/sqlexpress/archive/2012/06/12/10245900.aspx
- EF有内置缓存,如何进行缓存清理?
- 局部修改 http://stackoverflow.com/questions/12661881/exclude-property-on-update-in-entity-framework