[英]Base64 string saves in database as null C#
我在將圖像轉換為 Base64 字符串並將其保存為數據庫中的字符串時遇到問題,但它什么都不返回。 Base64Text 是全局變量,變量也不為空我用按鈕來填充文本框進行了測試,它只是將“”保存到數據庫中。
這是數據庫中表的模型
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public double ProductPrice { get; set; }
public int ProductAmount { get; set; }
public string ProductImage { get; set; } // Used for storing image string
public int userID { get; set; }
}
// Here is image converter
private void btnAddImage_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image Files(*.BMP;*.JPG;*.PNG;*.JPEG)|*.BMP;*.JPG;*.PNG;*.JPEG" +
"|All files(*.*)|*.*";
dialog.CheckFileExists = true;
dialog.Multiselect = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
var image = new Bitmap(dialog.FileName);
pictureBoxProductImage.Show();
pictureBoxProductImage.Image = (Image)image;
byte[] imageArray = System.IO.File.ReadAllBytes(dialog.FileName);
base64Text = Convert.ToBase64String(imageArray);
}
}
// Here is saving image using Entity framework
private void btnAddProduct_Click(object sender, EventArgs e)
{
string imagePath = base64Text;
if (txtBoxProductName.Text == null || txtBoxProductPrice.Text == null || txtBoxProductQuantity.Text == null || imagePath == null || imagePath == "")
{
MessageBox.Show("Please fill required information!", "", MessageBoxButtons.OK);
}
else
{
model.ProductName = txtBoxProductName.Text;
model.ProductPrice = Convert.ToDouble(txtBoxProductPrice.Text);
model.ProductAmount = Convert.ToInt32(txtBoxProductQuantity.Text);
model.ProductImage = imagePath;
model.userID = id;
using (var context = new ProductContext())
{
context.Products.Add(model);
context.SaveChanges();
}
MessageBox.Show("Product sucesffuly added to database!", "", MessageBoxButtons.OK);
Clear();
}
}
您看起來正在危險地混合變量范圍。 考慮到這段代碼:
model.ProductName = txtBoxProductName.Text;
model.ProductPrice = Convert.ToDouble(txtBoxProductPrice.Text);
model.ProductAmount = Convert.ToInt32(txtBoxProductQuantity.Text);
model.ProductImage = imagePath;
model.userID = id;
using (var context = new ProductContext())
{
context.Products.Add(model);
context.SaveChanges();
}
'model' 不限於此方法。
我不建議將視圖綁定到實體作為視圖模型,而是使用普通的 C# ViewModel 類來避免混淆和處理具有多個關注點的實體。 (屬性更改跟蹤、UI 驗證行為等)
它應該看起來更像:
using (var context = new ProductContext())
{
var product = new Product
{
UserId = id,
ProductName = txtBoxProductName.Text,
ProductPrice = Convert.ToDouble(txtBoxProductPrice.Text),
ProductAmount = Convert.ToInt32(txtBoxProductQuantity.Text),
ProductImage = hiddenImage.Text, // Something in the scope of the view storing the image Base64 content, not a global variable.
};
context.Products.Add(product);
context.SaveChanges();
}
添加實體時,您要確保處理的是該實體的新實例,而不是引用。 model
可能是對上下文已經跟蹤的另一個實體的引用,這意味着當您認為要添加新行時,最終可以替換完全不同記錄上的值。 此外,由於您正在確定新的 DbContext 實例的范圍,因此該范圍之外的任何實體引用都可能導致異常。
避免任何細節的全局變量。 選擇圖像時,請確保 Base64 存儲在應用程序頁面或綁定視圖模型范圍內的某處。 如果在這些更改之后仍然沒有 ProductImage 被保存,那么我會查看 Entity 聲明以確保它沒有被標記為Ignore
,並考慮使用探查器來查看正在生成的 SQL 以確保該字段被包括以及正在發送的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.