我想发现ASP.NET MVC生态系统中的新观点。所以我想尝试一下。
这是一个注册系统 4个表(但有7个表。您将在下面看到。) 顺便说一下,我正在使用DatabaseFirst风格。
我的模特:
public partial class AccountInfo
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Username { get; set; }
public Nullable<System.DateTime> DateSignUp { get; set; }
public string FacebookId { get; set; }
public string TwitterId { get; set; }
public string InstagramId { get; set; }
}
public partial class AccountPasswordOrder
{
public int Id { get; set; }
public int AccountId { get; set; }
public string Name { get; set; }
}
public partial class AccountAuth
{
public int Id { get; set; }
public int AccountId { get; set; }
public string Email { get; set; }
public int PasswordOrderId { get; set; }
}
public partial class AccountPassword1
{
public int Id { get; set; }
public int AccountId { get; set; }
public string Password { get; set; }
}
public partial class AccountPassword2
{
public int Id { get; set; }
public int AccountId { get; set; }
public string Password { get; set; }
}
public partial class AccountPassword3
{
public int Id { get; set; }
public int AccountId { get; set; }
public string Password { get; set; }
}
public partial class AccountPassword4
{
public int Id { get; set; }
public int AccountId { get; set; }
public string Password { get; set; }
}
这就是我的结构。 在我的代码中,
AccountInfo
表格,然后将其ID设为AccountId
,AccountPasswordOrder
表格,然后将其ID设为PasswordOrderId
,AccountAuth
表格中插入了值AccountId
,Email
和PasswordOrderId
)将密码分为4个部分,如下所示
string Password_1 = Form_Password.Substring(0, 10);
string Password_2 = Form_Password.Substring(10, 10);
string Password_3 = Form_Password.Substring(20, 10);
string Password_4 = Form_Password.Substring(30, 10);
然后 我不知道该怎么做。 因为我不知道如何做到这一点:
foreach (var item in PasswordOrderList)
{
string TableName = "AccountPassword" + item;
//how can i use TableName string variable for
//AccountPassword1 AccountPassword1Object=new AccountPassword1();
//AccountPassword1Object.AccountId=..
//AccountPassword1Object.Password=Password_+item;
//DB.AccountPassword1.Add(AccountPassword1);
//DB.SaveChanges();
}
答案 0 :(得分:0)
虽然这没有提供额外的安全性,但我一直在思考如何实现这一点,我想知道以下是否可能:
var passwords = new List<string>();
passwords.Add(Form_Password.Substring(0, 10));
passwords.Add(Form_Password.Substring(0, 10));
passwords.Add(Form_Password.Substring(10, 10));
passwords.Add(Form_Password.Substring(20, 10));
passwords.Add(Form_Password.Substring(30, 10));
var dbContext = new MyDbContext();
for (var i = 0; i < passwords.Count(); i++)
{
// Get the password section to store
var passwordString = passwords[i];
// Generate fully qualified namespace of the EF object that represents the table, inserting the password count number
var tableName = string.Format("Namespace.To.EntityFramework.AutoGenerated.Objects.AccountPassword{0}, Assemblyname", i + 1);
// Get the type using fully qualified namespace
var tableType = Type.GetType(tableName);
// Get instance of type and assign to dynamic
dynamic password = Activator.CreateInstance(tableType);
// Set the properties of the object
password.Password = passwordString;
// other properties...
// password.AccountId = 100;
// Add them to the DbContext
var dbContext.Set(tableType).Add(password);
}
dbContext.SaveChanges();
MyProject
的项目中。构建项目时生成的DLL是MyProject.dll
。
这是您的实体框架自动生成的课程,因为您首先使用EF数据库:
namespace MyAmazingApp.Entities
{
public partial class AccountPassword1
{
public int Id { get; set; }
public int AccountId { get; set; }
public string Password { get; set; }
}
}
此类的完全限定名称空间为:
MyAmazingApp.Entities.AccountPassword1,MyProject
所以你需要将string.Format()
中的值替换为,例如:
// Generate fully qualified namespace of the EF object that represents the table, inserting the password count number
var tableName = string.Format("MyAmazingApp.Entities.AccountPassword{0}, MyProject", i + 1);