這次作業(yè)需要做的是一個簡單的
可以Create Account List
Account List
需要有寫Extra Code
一般來說不能Delete
所有Main Page都自動生成了
Change View
Change Controller 的一些內(nèi)容
不需要build application
這個作業(yè)也需要Login
但是已經(jīng)是有l(wèi)ogin
作業(yè)需要要求的
Account是
Transaction是個很重要的內(nèi)容在這次作業(yè)里
需要知道User是Authorize去看到page
聽起來很復(fù)雜砂碉,但是并不難
只需要用一個 [Authorize] attribute就可以了
我的問題:
- 如何使得 URL 是 suffix /Bank/Account/Create ? suffix 是什么
- 如何把內(nèi)容存在數(shù)據(jù)庫里当娱,MVC是否也要重新搭載一個數(shù)據(jù)庫
- 如何放在Azure之上
public class Client
{
public Client()
{
this.Accounts = new HashSet<Account>();
}
public string UserName { get; set; }
public string Password { get; set; }
public virtual ICollection<Account> Accounts { get; set; }
}
public class Account
{
public Account()
{
this.Transactions = new HashSet<Transaction>();
}
public string AccountName { get; set; }
public float AccountBalance { get; set; }
public virtual Client Client { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }
}
public class Transaction
{
public int TransactionNumber { get; set; }
public string AccountName { get; set; }
public virtual Account Account { get; set; }
}
-
創(chuàng)建一個新的C#Web項(xiàng)目,命名為Bank
在新建ASP.Net項(xiàng)目對話框中,選擇MVC模板并點(diǎn)擊更改身分驗(yàn)證按鈕闺骚,選擇無身份驗(yàn)證并確定秒赤,創(chuàng)建項(xiàng)目致讥。但是老師上課選擇的是第二項(xiàng) Individual User Account
- 將對站點(diǎn)目錄,布局和主頁面做些細(xì)微簡單的改變顿锰,打開 _Layout.cshtml并做以下更改:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - ASP Bank System</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("ASP Bank", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("Account", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - ASP Bank</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
- 在View\Home\Index.cshtml中谨垃,用下面的代碼替換原有的
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>ASP Bank</h1>
<p class="lead">At ASP Bank, we have a clear purpose to help make financial lives better through the power of every connection. We fulfill this purpose through a strategy of responsible growth, which includes a focus on environmental, social and governance leadership.</p>
<p><a class="btn btn-primary btn-lg">Learn more »</a></p>
</div>
<div class="row">
<div class="col-md-4">
<h2>OnLine Banking</h2>
<p>
With Online Banking you can check your balance and account activity or search your statements and payment history whenever you like from your laptop, tablet or smartphone. You can also create email and text alerts that let you know if your account balance is low or if a payment is due to help you avoid fees.
</p>
<p><a class="btn btn-default" >Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Pay and Transfer</h2>
<p>At your convenience—pay bills and make transfers online from home, the office, wherever you can connect. Transfer money securely and quickly between your own accounts or to your friends and family even if they have accounts at other banks. You can also schedule bills in advance knowing they’ll be paid securely.</p>
<p><a class="btn btn-default" >Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Mobile Banking</h2>
<p>Access your ASP Bank accounts straight from your mobile device. Check your balance, deposit checks, pay bills, transfer money and get cash-back deals! </p>
<p><a class="btn btn-default" >Learn more »</a></p>
</div>
</div>
上述都還是前端的內(nèi)容
下面是關(guān)于EntityFramework的內(nèi)容
- 在工具菜單中硼控,點(diǎn)擊NuGet程序包管理器乘客,點(diǎn)擊程序包管理器控制臺。
PM> Install-Package EntityFramework
Attempting to gather dependency information for package 'EntityFramework.6.2.0' with respect to project 'Bank', targeting '.NETFramework,Version=v4.6.1'
Gathering dependency information took 13.28 sec
Attempting to resolve dependencies for package 'EntityFramework.6.2.0' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package 'EntityFramework.6.2.0'
Resolved actions to install package 'EntityFramework.6.2.0'
Retrieving package 'EntityFramework 6.2.0' from 'nuget.org'.
Removed package 'EntityFramework.6.1.3' from 'packages.config'
Successfully uninstalled 'EntityFramework.6.1.3' from Bank
Adding package 'EntityFramework.6.2.0' to folder 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages'
Added package 'EntityFramework.6.2.0' to folder 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages'
Added package 'EntityFramework.6.2.0' to 'packages.config'
Executing script file 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages\EntityFramework.6.2.0\tools\init.ps1'
Executing script file 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages\EntityFramework.6.2.0\tools\install.ps1'
Type 'get-help EntityFramework' to see all available Entity Framework commands.
Successfully installed 'EntityFramework 6.2.0' to Bank
Removing package 'EntityFramework.6.1.3' from folder 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages'
Access to the path 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages\EntityFramework.6.1.3\tools\EntityFramework.PowerShell.Utility.dll' is denied.
Access to the path 'EntityFramework.PowerShell.Utility.dll' is denied.
Removed package 'EntityFramework.6.1.3' from folder 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages'
Executing nuget actions took 4.82 sec
Time Elapsed: 00:00:18.6145574
One or more packages could not be completely uninstalled: 'C:\Users\Kaiwei Zhang\Desktop\Homework4\Bank\packages\EntityFramework.6.1.3'. Restart Visual Studio to finish uninstall.
PM>
shan
這里我有個問題:之前在做Webform的時候淀歇,我們都直接可以install 為什么MVC要在console里輸入呢易核?
可能是因?yàn)槲覄偛旁趓un 頁面,停止之后浪默,又出現(xiàn)了三個選擇牡直。
在Tool —— NuGet Package Manager —— Manager For Solution
選擇第一個EF Package缀匕。
還有個問題是:MVC 可以用model first 嗎?只有在使用Code Frist 都需要添加NuGet碰逸。
- 注意這次作業(yè)里乡小,Account和Transaction存在一對多的關(guān)聯(lián)關(guān)系。同樣Account和User也是如此饵史。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Bank.Models
{
public class Client
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime RegisterDate { get; set; }
public virtual ICollection<Account> Accounts { get; set; }
}
}
ICollection<Account>
會報錯
ID屬性將成為數(shù)據(jù)表中對應(yīng)該類的主鍵列 primary key column of the database table 满钟。
默認(rèn)情況下,Entity Framework 將命名為ID或類名+ID的屬性用作主鍵列胳喷。Account屬性是一個導(dǎo)航屬性 navigation property湃番。導(dǎo)航屬性建立本實(shí)體entity 相關(guān)的其他實(shí)體entity 之間的聯(lián)系。如果一個Client 在數(shù)據(jù)庫中有兩個相關(guān)的Account行(列使用使用該client 的主鍵值作為外鍵)吭露,即Client 實(shí)體的Account 屬性將包含這兩個關(guān)聯(lián)的Account 實(shí)體吠撮。
如果某個導(dǎo)航屬性可以包含多個實(shí)體(如多對多或一對多關(guān)系),它的類型必須可以進(jìn)行增刪改操作讲竿,比如ICollection泥兰。
- 有關(guān)Account 的內(nèi)容
屬性 | 要求 |
---|---|
Account Number | 7位數(shù)字,不可有相同的Account Number |
Check Account Balance | 起始默認(rèn)值是0 |
Check Account Name | 用戶可以自定義賬戶名 enter a descriptive name题禀, 命名就會自定義生成 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Bank.Models
{
public class Account
{
public int AccountID { get; set; }
public string AccountName { get; set; }
public int AccountNumber { get; set; }
public int AccountBalance { get; set; }
public virtual Client Client { get; set; }
public virtual ICollection<Transaction> Transaction { get; set; }
}
}
ID屬性將成為數(shù)據(jù)表中對應(yīng)該類的主鍵列鞋诗。默認(rèn)情況下,實(shí)體框架將命名為ID或類名+ID的屬性用作主鍵列迈嘹。我這里寫了一個AccountID來做Primary Key师脂。
ClientID是一個外鍵,相應(yīng)的導(dǎo)航屬性是Client江锨。一個Account實(shí)體關(guān)聯(lián)到一個Client實(shí)體。所以屬性只能容納一個Client實(shí)體(而不像之前的Client.Account導(dǎo)航屬性糕篇,它可以容納多個Account實(shí)體)啄育。
這一點(diǎn)我有點(diǎn)暈,一會執(zhí)行一下來驗(yàn)證:如果一個屬性的命名方式為導(dǎo)航屬性名+主鍵屬性名拌消,實(shí)體框架便會將該屬性視為外鍵屬性挑豌。(例如,Client 實(shí)體的主鍵為ID墩崩,則ClientID被視為為Client導(dǎo)航屬性的外鍵)氓英。
- 有關(guān)Transaction 內(nèi)容
屬性 | 要求 |
---|---|
Date | |
Amount of the Transaction | 交易數(shù)額 All decimals must be displayed to 2 decimal places 負(fù)數(shù)括號 |
Balance | 交易后余額 All decimals must be displayed to 2 decimal places 負(fù)數(shù)括號 |
Description | 交易內(nèi)容 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Bank.Models
{
public class accounts
{
public int AccountID { get; set; }
public string AccountName { get; set; }
public int AccountNumber { get; set; }
public int AccountBalance { get; set; }
public int ClientID { get; set; }
public virtual Client Client { get; set; }
public virtual ICollection<Transaction> Transaction { get; set; }
}
}
- 關(guān)于DatabaseGenerated特性,需要使用
- 注意:如果你在這些實(shí)體類全部創(chuàng)建完成之前嘗試運(yùn)行項(xiàng)目鹦筹,你將會得到編譯器錯誤的提示铝阐。
建數(shù)據(jù)庫上下文 Database Context
The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the System.Data.Entity.DbContext class. In your code you specify which entities are included in the data model. You can also customize certain Entity Framework behavior. In this project, the class is named SchoolContext.
在一個數(shù)據(jù)模型中負(fù)責(zé)協(xié)調(diào)實(shí)體框架功能的主類被稱為數(shù)據(jù)庫上下文類。您可以通過派生自System.Data.Entity.DbContext類來創(chuàng)建铐拐。你可以在代碼中指定那些實(shí)體被包含在數(shù)據(jù)模型中徘键。您可以可以自定義某些實(shí)體框架的行為练对。在本項(xiàng)目中,上下文類被命名為BankContext吹害。
右鍵單擊解決資源方案管理器中的項(xiàng)目螟凭,點(diǎn)擊添加,點(diǎn)擊新建文件夾它呀。將新文件夾命名為DAL(數(shù)據(jù)訪問層)螺男。在該文件夾中創(chuàng)建一個SchoolContext類并使用以下代碼替換模板代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bank.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace Bank.DAL
{
public class BankContext : DbContext
{
public BankContext() : base("BankContext")
{
}
//指定連接字符串,連接字符串(稍后將被添加到web.config文件中)的名稱被傳遞給構(gòu)造函數(shù)纵穿。
public DbSet<Client> Clients { get; set; }
public DbSet<Account> Accounts { get; set; }
public DbSet<Transaction> Transactions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
指定實(shí)體集:這段代碼為每個實(shí)體集合創(chuàng)建了一個DbSet屬性下隧。在實(shí)體框架中,一個實(shí)體集對應(yīng)數(shù)據(jù)庫中的表政恍,一個實(shí)體對應(yīng)數(shù)據(jù)表中的一行汪拥。
你可以省略DbSet<Account> 和DbSet<Transaction>,實(shí)體框架將自動把它們包含進(jìn)來篙耗。因?yàn)镃lient 實(shí)體引用了Account實(shí)體迫筑,并且Account實(shí)體引用了Transaction實(shí)體。
同樣可以通過傳遞連接字符串而不是存儲在web.config文件的連接字符串名稱本身來指定連接宗弯。如果你不指定連接字符串或一個明確的名稱脯燃,實(shí)體框架將假定連接字符串名稱和類名稱一致,即在本例中蒙保,默認(rèn)的連接字符串名稱為BankContext辕棚,同你顯示聲明的一致。
指定表名: OnModelCreating方法中的modelBuilder.Convertions.Remove被用來防止生成復(fù)數(shù)表名邓厕。如果你不這樣做逝嚎,在數(shù)據(jù)庫中生成的數(shù)據(jù)表將被命名為Clients,Accounts及Transactions。相反详恼,在本例中我們的表名是Student,Course及Enrollment补君。對于表名稱是否應(yīng)該使用復(fù)數(shù)或單數(shù)命名模式并沒有明確的要求。在本教程中我們將使用單數(shù)形式昧互。重要的一點(diǎn)是挽铁,你可以選擇任意的命名方式——通過是否注釋掉該行代碼。
設(shè)定初始化數(shù)據(jù)庫并填充測試數(shù)據(jù)
當(dāng)運(yùn)行程序時敞掘,實(shí)體框架可以自動創(chuàng)建(或自動刪除并重新創(chuàng)建)數(shù)據(jù)表叽掘。
可以指定這應(yīng)該在每次程序運(yùn)行時進(jìn)行或僅當(dāng)模型發(fā)生了變化而不與現(xiàn)有的數(shù)據(jù)庫同步時才進(jìn)行。
也可以寫一個Seed方法玖雁,以便在數(shù)據(jù)庫初始化后自動填充測試數(shù)據(jù)到新的數(shù)據(jù)表中更扁。
默認(rèn)的行為是只有當(dāng)該數(shù)據(jù)庫不存在時才創(chuàng)建(當(dāng)數(shù)據(jù)庫已經(jīng)存在時會拋出一個異常)。
本節(jié)將指定在每次模型發(fā)生變化時都刪除舊數(shù)據(jù)庫并建立一個新的。
在本例中這樣做是適當(dāng)?shù)摹?strong>Seed方法將在重新創(chuàng)建后自動填充測試數(shù)據(jù)疯潭。而在生產(chǎn)中通常不希望這樣做從而丟失數(shù)據(jù)庫中的所有數(shù)據(jù)赊堪。
稍后將看到如何使用Code First Migration來改變數(shù)據(jù)庫架構(gòu),而不是刪除并重新創(chuàng)建竖哩。
在DAL文件夾中哭廉,創(chuàng)建一個BanklInitializer類并使用以下代碼替換默認(rèn)的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bank.Models;
namespace Bank.DAL
{
public class BankInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<BankContext>
{
protected override void Seed(BankContext context)
{
var clients = new List<Client>
{
new Client{FirstName="Da",LastName="Huo",RegisterDate=DateTime.Parse("2005-09-01")},
new Client{FirstName="Kaiwei",LastName="Zhang",RegisterDate=DateTime.Parse("2002-09-01")},
};
clients.ForEach(s => context.Clients.Add(s));
context.SaveChanges();
var accounts = new List<Account>
{
new Account{
AccountID = 1,
AccountName = "Checking",
AccountNumber = 0000001,
AccountBalance = 0,
},
new Account{
AccountID = 2,
AccountName = "Saving",
AccountNumber = 0000002,
AccountBalance = 0,
},
};
accounts.ForEach(s => context.Accounts.Add(s));
context.SaveChanges();
var transactions = new List<Transaction>
{
new Transaction{
TransactionID = 1,
TransactionDate = DateTime.Parse("2018-04-01"),
TransactionAmount = 0,
TransactionBalance = 0,
},
new Transaction{
TransactionID = 2,
TransactionDate = DateTime.Parse("2018-04-02"),
TransactionAmount = 0,
TransactionBalance = 0,
},
};
transactions.ForEach(s => context.Transactions.Add(s));
context.SaveChanges();
}
}
}
Seed方法將數(shù)據(jù)庫的上下文對象作為輸入?yún)?shù),并在方法中的代碼使用該上下文對象將新的實(shí)體添加到數(shù)據(jù)庫中相叁。對于每個實(shí)體模型遵绰,代碼創(chuàng)建新的實(shí)體集合添加他們到相應(yīng)的DbSet屬性,然后將更改保存到數(shù)據(jù)庫增淹。它并不是必須在每組實(shí)體后立即調(diào)用SaveChanges方法椿访。但這樣做有助于你在發(fā)生數(shù)據(jù)庫寫入異常時快速找到問題的根源。
向web.config中添加一個元素來告訴實(shí)體框架你將使用初始化類虑润,比如下面的例子:
<contexts>
<context type ="Bank.DAL.BankContext, Bank">
<databaseInitializer type="Bank.DAL.BankeInitializer, Bank">
</databaseInitializer>
</context>
context 的Type屬性指定了上下文的類名成玫。你應(yīng)當(dāng)使用完整的類名和程序集名。同樣databaseInitializer的Type指定了初始化類的名稱拳喻。(如果你不想使用EF初始化哭当,你可以在上下文元素中設(shè)置disableDatabaseInitialization="true")。
作為一種在web.config中設(shè)置初始值設(shè)定項(xiàng)的替代方法冗澈,你可以通過在Global.asax.cs中Application_Start方法中增加Database.SetInitializer語句來實(shí)現(xiàn)同樣的功能钦勘。
現(xiàn)在應(yīng)用程序已經(jīng)設(shè)置為在程序首次運(yùn)行時,對模型和數(shù)據(jù)庫中的表進(jìn)行比較亚亲,如果有區(qū)別彻采,應(yīng)用程序刪除并重新創(chuàng)建該數(shù)據(jù)庫。
注意:當(dāng)你將應(yīng)用程序部署到生產(chǎn)環(huán)境中時捌归,你必須刪除或禁用數(shù)據(jù)庫重新創(chuàng)建代碼肛响,后面的教程會演示這一點(diǎn)。
SQL Server Express LocalDB數(shù)據(jù)庫
LocalDB是SQL Server Express的一個輕量版本惜索,非常適合用來進(jìn)行本地測試特笋,但不建議在生產(chǎn)中使用。
打開應(yīng)用程序的web.config文件门扇,添加數(shù)據(jù)庫連接字符串,如下面的例子:
這一段我確實(shí)有點(diǎn)云里霧里偿渡,因?yàn)楫?dāng)時老師去做得時候是直接在SQL Server上做的臼寄。
創(chuàng)建 Controller 和 View
終于到了這一步,可以驗(yàn)證之前那么長的步驟有沒有出錯了溜宽。吉拳。。
激動不适揉?留攒!
右鍵點(diǎn)擊Controllers文件夾煤惩,選擇添加,單擊新建搭建基架項(xiàng)炼邀。
But ... 在我耗盡了不少精力后...各種修改魄揉,各種rebuild之后...
發(fā)現(xiàn)只是...我更具教程在 web.config里多加了一行見鬼的內(nèi)容,本身就有的...
<connectionStrings>
<add name="BankContext" connectionString="Data Source = (LocalDb)\v11.0;Initial Catalog=Bank1;Integrated Security = SSPI;"
providerName ="System.Data.SqlClient"/>
</connectionStrings>
刪除之后就...順利的scaffolding出了內(nèi)容拭宁,不要高興太早...
我們檢查一下內(nèi)容的真實(shí)性..
基架目前其實(shí)只是將創(chuàng)建client控制器和一組視圖(.cshtml文件)洛退。
使用實(shí)體框架創(chuàng)建項(xiàng)目時還可以獲得一些附加功能:只需創(chuàng)建第一個模型類而無需創(chuàng)建連接字符串,然后在添加控制器時指定新的上下文類杰标。
該基架將創(chuàng)建數(shù)據(jù)庫上下文類和連接字符串兵怯,以及控制器和視圖。在VS中打開ClientController.cs文件腔剂,你將看到類中已經(jīng)有一個實(shí)例化的數(shù)據(jù)庫上下文對象:
Index方法從數(shù)據(jù)庫dbcontext 實(shí)例的Students屬性讀取Student實(shí)體集以獲取Student列表
public ActionResult Index()
{
return View(db.Clients.ToList());
}
后來我把Initializer刪掉了媒区,反正之后也不能用,還是要使用migration
輾轉(zhuǎn)反側(cè),我把SQL Server里的表刪除了再run了一次赞赖。
又可以了夫偶。
感覺之前的邏輯就是說,我本來就Initialize了噪服,然后再添加就不可以?問號臉胜茧。
反正這次嘗試是沒有問題了粘优。
但是存在另一個問題就是在SQL Server里又沒有了數(shù)據(jù)記錄。
更新:再打開一次SQL Server就有了數(shù)據(jù)了呻顽。
...終于可以做下一步了...上面都是多簡單的步驟雹顺,卡了巨久,手笨的人不適合生存廊遍。
我如何驗(yàn)證我已經(jīng)做到了Migration呢嬉愧??喉前?我并不覺得我做到了migration没酣。。卵迂。
默認(rèn)情況下裕便,當(dāng)我們使用Entity Framework Code First 自動創(chuàng)建一個數(shù)據(jù)庫,像我們之前教程中講的那樣见咒,Code First 添加一個table幫我們跟蹤數(shù)據(jù)庫結(jié)構(gòu)是否與模型類同步偿衰。如果不同步,Entity Framework 將拋出一個錯誤,這樣更方便我們在開發(fā)的時候發(fā)現(xiàn)問題下翎,否則只能在運(yùn)行時通過晦澀的錯誤來查找了缤言。
之前出的問題是,我一開始多手寫了一個 RegistrationDate 變量视事,后來刪除了胆萧,就改變了EF。
我閱讀到一個知識:
我們修改了我們的EF模型
- 在類里修改添加變量郑口,編譯解決方案
- 修改后的view里的cshtml
- 數(shù)據(jù)表還沒有修改TП獭!犬性!
- 解決這個問題有以下幾種途徑:
- 讓Entity Framework自動刪除并根據(jù)新的模型自動創(chuàng)建數(shù)據(jù)庫瞻离。這種方式在早起開發(fā)過程中的測試數(shù)據(jù)庫中非常方便,它可以快速的修改模型和數(shù)據(jù)庫結(jié)構(gòu)乒裆。另一方面套利,這樣做將會使你丟失已有的數(shù)據(jù),因此這種方式不能用在生產(chǎn)環(huán)境的數(shù)據(jù)庫中鹤耍。
- 在數(shù)據(jù)庫中加上新增加的字段肉迫,使數(shù)據(jù)庫和Model類的結(jié)構(gòu)相同。這種方式的優(yōu)點(diǎn)是能夠保留數(shù)據(jù)稿黄,你可以手動修改或使用數(shù)據(jù)庫腳本修改喊衫。
- 使用Code First 遷移來升級數(shù)據(jù)庫結(jié)構(gòu)。migration詳細(xì)
這個界面很智能的地方在于 ClientID就是Foreign Key杆怕。
- 但是最后我們要的效果肯定是 如何在我Login之后就不顯示這個部分族购。
- Account Number需要自動生成嗎? 七位數(shù)不同值陵珍。
作業(yè)細(xì)節(jié)要求
- A bank accounts listing with URL suffix /Bank/Account/List, shows all of the accounts presently open.
URL后綴 /Bank/Account/List 的銀行賬戶列表顯示當(dāng)前打開的所有賬戶寝杖,所以就是登錄就可以看到自己的所有賬戶
Hint: implement this as a form with just a submit button. Create two controller actions:
one action for GET requests, which asks the user to confirm by pressing a button. Create a
second one adorned with the [HttpPost] attribute that processes the form and performs the
“deletion” and informs the user that is has been deleted (or error, if there was an error).
- 兩個 Controller Action
When deleting an account, you should not actually delete it from the database, but rather flag it to be hidden using a Boolean in the model for the account. Once an account is “deleted” (i.e., hidden), the system should behave as if the account was in fact deleted from the database. For example, it should never again be viewable or accessible in any way through the user interface, nor should any transactions for that account be viewable. An account must have a $0.00 balance prior to deletion.
因?yàn)橐婚_始做的時候沒有考慮到這個條件,沒有在Account的Model里添加 Bool 變量互纯。
后來添加了再去搭腳手架的時候瑟幕,我猜應(yīng)該是migration的問題就有了下圖:
畢竟原來存的數(shù)據(jù),現(xiàn)在就缺少了一個bool值
A create account screen with URL suffix /Bank/Account/Create.
Starts a new checking account with a $0.00 balance. The user should be able to enter a descriptive name for the account (e.g. “Mary’s Main Checking Account”) and account number of the new checking account to be created.
The account number must be exactly 7 digits and be unique among all other accounts at the bank. When finished, this should take the user back to the bank accounts listing above.
- 用戶可以直接命名
- 但是默認(rèn)起始余額是 $0
- account number 也需要做用戶做初始化
- account number 需要7位數(shù)留潦,不可以已經(jīng)存在
- 完成賬戶create只盹,轉(zhuǎn)回到 listing view
An account details screen (/Bank/Account/{id}/Transaction/List),
which shows the account name, account number, current account balance,
and a listing of the 10 most recent transactions for the account specified in the URL. Each transaction should show the date, description, and amount of the transaction as well as a balance. There should be a “Create Transaction” button that takes the user to the new transaction screen, discussed below. An example of the transaction list is shown below in Table 1. You may show the comma in the numeric formatting, but this is not a requirement.
- Account Detail的界面包括了account name, account number, current account balance
- 包括了最近10次的交易
- 每筆交易要顯示日期,描述兔院,交易金額殖卑,余額
- 需要有 “Create Transaction”
- may show the comma in the numeric formatting
However, all decimals must be displayed to 2 decimal places.
See String.Format for ideas. Note that negative amounts are typically shown by enclosing the amount in parentheses.
Clicking on a transaction should take you to the transaction details screen,
discussed below.
- 都要是2位小數(shù),負(fù)號用括號表示
- 點(diǎn)擊 transaction 可以到 transaction頁面
A new transaction screen (/Bank/Account/{id}/Transaction/Create), in which the user can create a new transaction for the account specified in the URL. Each transaction shall consist of at least: a globally unique primary key serving as a transaction number (database generated and not visible on this screen), the date and time of the transaction (need not be shown on the screen), a textual description of the transaction, and the amount of the transaction expressed as a positive decimal. A separate drop down list on the screen which is statically populated with the options “Check” or “Deposit” will indicate the type of transaction.
Although this is how it should appear on the screen, you may represent check or deposit transactions however you wish in the model.
- 新的交易屏幕 (/Bank/Account/{id}/Transaction/Create)
- 用戶可以在該屏幕中為URL中指定的賬戶創(chuàng)建新的交易秆乳。
- 每筆交易至少應(yīng)包括:作為transaction number--- unique primary key(數(shù)據(jù)庫已生成并且在此屏幕上不可見)
- 交易的日期和時間(不需要在屏幕上顯示)
- 文本描述交易以及交易金額表示為正數(shù)小數(shù)懦鼠。
- 在屏幕上單獨(dú)的drop down list 中靜態(tài)填充選項(xiàng)“Check” or “Deposit”將顯示交易類型。
- Although this is how it should appear on the screen, you may represent check or deposit transactions however you wish in the model. 這句話沒有讀懂
There should exist a Submit and Cancel button at the bottom of the screen. Transactions cannot be deleted once created, though they should be completely hidden from view if the parent account is deleted.
Upon pressing the Submit button, the following validation should occur: all visible fields should be required.
The transaction cannot be for $0.00, less than -$5,000.00 or more than $5,000.00.
Transactions that would bring the account balance below $0.00 are not permitted.
If valid, the transaction should be recorded and the user taken to the account transactions screen, described below. If the user presses the Cancel button, they should be directed to the account details screen described above.
- 屏幕底部應(yīng)該有一個提交和取消按鈕屹堰。
- 交易無法在創(chuàng)建后刪除肛冶,但如果父賬戶被刪除,它們應(yīng)該完全隱藏扯键。
- 按提交按鈕后睦袖,應(yīng)該發(fā)生以下驗(yàn)證:all visible fields should be required
- 該交易不能為$ 0.00,小于 - $ 5,000.00或超過$ 5,000.00
- 不允許將賬戶余額低于0.00美元的交易
- 如果有效荣刑,應(yīng)記錄交易并將用戶帶到 account transactions screen
- 如下所述馅笙。如果用戶按下“取消”按鈕,則應(yīng)將其指向上述帳戶 account details screen
所以交易也應(yīng)該有 IsDeleted 這個bool
A transaction details screen (/Bank/Account/{id1}/Transaction/{id2}) where {id1} is a the account number and {id2} is the transaction number. You should validate that the transaction corresponds to the account in question. The transaction details screen should provide a “read only” 3 view of the transaction: date and time of the transaction (month, day, and year, hour and minute), description of the transaction, and the amount of the transaction expressed as a decimal (positive for deposit, negative for check). A button should take the user back to the account details screen. Users should under no circumstance be allowed to view accounts that are not theirs or transactions for accounts that are not theirs. Other requirements are as follows:
- 交易詳情屏幕((/Bank/Account/{id1}/Transaction/{id2})厉亏,其中{id1}是賬戶號碼董习,{id2}是交易號碼。
這個有點(diǎn)不懂
您應(yīng)該驗(yàn)證交易是否與相關(guān)賬戶相符爱只。交易詳情屏幕應(yīng)提供交易的“只讀”3視圖:交易的日期和時間(月皿淋,日,年恬试,小時和分鐘)窝趣,交易描述以及交易金額十進(jìn)制(存款為正,支票為負(fù))训柴。
一個按鈕應(yīng)該將用戶帶回到帳戶詳細(xì)信息屏幕哑舒。在任何情況下,用戶都不應(yīng)允許查看不屬于他們的賬戶或不屬于他們的賬戶的交易幻馁。其他要求如下:
The solution must be deployed to Azure. Write the hyperlink of the site inline in your homework submission. If you had any issues deploying to Azure previously, you should make resolving those issues a priority. Do not expect last minute help.
Your models relating to users, bank accounts, and transactions must be stored in the database using Entity Framework.
Because this is a banking web site, assume all transactions are sensitive. For this reason, ensure that your site is protected against cross-site request forgeries. This is as simple as displaying the anti-forgery token in your views and validating the token in your actions. Scaffolding will likely do this for you, but it is your responsibility to ensure that the finished product contains the proper checking.
- cross-site request forgeries是什么洗鸵?
- 要確保
For the purposes of this assignment, it is not necessary to tier the solutionn into layers (data store, model, business logic, UI). Everything can be kept in one project this time. However, all business logic code should be placed into a folder called “Biz Logic.” It is okay for business logic to directly perform database operations in this homework. However, it is a better design to decouple these—we will cover the topics of decoupling and dependency injection in a future lecture.
- 不理解這句話
A user should only be able to see their own accounts and transactions for only those accounts. Specifically, accounts that were created while they were logged into the web site under their user ID.
You don’t have to write any login functionality as part of this assignment. You can (and should) use the built-in authentication features in the template MVC site generated by Visual Studio. To do this, be sure
to specify “individual user accounts” when creating your project so that these features will be included. The Visual Studio MVC project template uses ASP.NET Identity for user management.
- “individual user accounts”
- ASP.NET Identity for user management.
You can query the unique ID of the currently logged in user with User.Identity.GetUserId().
You may maintain your own user table and reference the user ID from Identity. No foreign key is necessary between your user table and Identity’s.
- 這句話解決我之前的疑惑,但是我還是完全不理解
Each user must log into their account to access all of the functionality described above. That is, authorization is required for all pages discussed above. At your discretion, you may choose to have additional “public” pages not described here, such as an “About” page—those types of screens do not require authorization.
You may (and should) use the scaffolding feature to create views and/or controllers as necessary, and then customize the scaffolded views and controllers, accordingly. Much of the functionality required above is provided to you “out-of-the-box” by the scaffolding feature, although you may need to make some adjustments. Inspecting the auto-generated MVC code allows you to learn how to perform typical operations in MVC.
You will need to register new routes for this assignment, so familiarize yourself with RouteConfig.cs (included in new MVC projects) and educate yourself on routing first. Consult the NYU Classes Resources folder, lectures, and demos too.
這句話也完全不懂宣赔。
The web site does not have to look beautiful. It can look “basic” as long as it is both usable and clean in appearance and function. The code should be written in a manner one would expect of a graduate computer science (or related major) student. This assignment is mostly about using the MVC features and is therefore more about the code than the screen appearance.
Make ample use of comments to inform code readers what you are trying to accomplish. Keep code short and concise
Question Remain:
- 關(guān)于DatabaseGenerated特性预麸,需要使用
- Code First Migration來改變數(shù)據(jù)庫架構(gòu)
- 我如何檢查我的數(shù)據(jù)庫建立的Entity關(guān)系是不是正確呢?
- 現(xiàn)在initializer 無法工作儒将,我刪除了之后可以工作
- 但是需要我寫入日期吏祸?
相關(guān)鏈接:
建立一個EF數(shù)據(jù)模型