gpt4 book ai didi

c# - 当我已经有了我的模型和数据库时,我可以使用 Entity Framework 吗?

转载 作者:行者123 更新时间:2023-11-30 22:21:27 24 4
gpt4 key购买 nike

在我工作的 WPF 应用程序中,我已经有了我的模型和 sqlce 数据库。但是现在我使用普通的参数化查询来检索、更新或删除我的数据。

我还能为此使用 Entity Framework 吗?还是为时已晚?我的模型实现了 INotifyChangedProperty(MVVM 应用程序)。

现在我的 AddressModel 中的插入方法示例。我想使用 Entity Framework 来改变这一点。

    public int InsertNewAddress(bool isnew)
{
IsNew = isnew;
try
{
ArrayList paramList = new ArrayList();
paramList.Add(new SqlCeParameter() { ParameterName = "@Id", Value = Id, DbType = DbType.Guid, Size = 16, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Street", Value = Street, DbType = DbType.String, Size = 50, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Number", Value = Number, DbType = DbType.String, Size = 10, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Bus", Value = DBNull.Value, DbType = DbType.String, Size = 5, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@ZipCode", Value = Zipcode, DbType = DbType.String, Size = 10, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@City", Value = City, DbType = DbType.String, Size = 50, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Created", Value = DateTime.Now, DbType = DbType.DateTime, Size = 23, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Modified", Value = DateTime.Now, DbType = DbType.DateTime, Size = 23, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Country", Value = Country, DbType = DbType.String, Size = 50, Direction = ParameterDirection.Input });

StringBuilder sb = new StringBuilder();

if (IsNew) // new address, insert
{
sb.Append("INSERT INTO [RF_Address] ");
sb.Append("([Id],[Street],[Number],[Bus],[ZipCode],[City] ,[DateCreated],[DateModified], [Country]) ");
sb.Append("VALUES ");
sb.Append("(@Id, @Street, @Number, @Bus, @ZipCode, @City, @Created, @Modified, @Country)");
}
else
{
sb.Append("UPDATE [RF_Address] ");
sb.Append("SET ");
sb.Append("[Street] = @Street, [Number] = @Number, [Bus] = @Bus, [ZipCode] = @ZipCode, [City] = @City, ");
sb.Append("[DateModified] = @Modified, [Country] = @Country ");
sb.Append("WHERE [Id] = @Id");
}

int result = SqlCeHelper.ExecuteNonQuery(sb.ToString(), paramList);
if (result != 1)
throw new CustomException("Insert address failed!");

return result;
}
catch (CustomException appEx)
{
throw new CustomException(appEx.Message, appEx);
}
catch (Exception ex)
{
throw new Exception("Error InsertNewAddress: " + ex.Message, ex);
}
}

谢谢!

最佳答案

是的,你可以。

Entity Framework 有三种工作流程:Database FirstModel FirstCode First,所以在这三者中你可以使用Database首先

关于c# - 当我已经有了我的模型和数据库时,我可以使用 Entity Framework 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14374595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com