The code works with EF6 and Oracle.ManagedDataAccess.EntityFramework.
With Core 3.1 the following error occurs by saving the context:
An error occurred while updating the entries. See the inner exception for details.
OracleException: ORA-01460: Nicht implementierte oder nicht sinnvolle Umwandlung gefordert
My DB-Set for the images looks like this:
public class Picture
{
public int PictureID { get; set; }
public byte[] Bild { get; set; }
public DateTime Ludsys { get; set; }
public int OrderDetailID { get; set; }
public OrderDetail OrderDetail { get; set; } // Navigationseigenschaft
} // Picture
When I call "Add-Migration" und "Update-DataBase" at the PM-Console the System generates for the byte-array a Column of type RAW instead of BLOB.
Code in .xaml File:
<Image Name="imgBild" Source="{Binding Bild, Converter={StaticResource PhotoConverter}, Mode=TwoWay}" Grid.Row="2"
Height="400" Margin="10,0" HorizontalAlignment="Left" Stretch="Uniform"/>
Code in .xaml.cs File:
...
picture = new Picture
{
Bild = null,
Ludsys = DateTime.Now,
OrderDetail = orderdetail
}; // picture
imgBild.DataContext = picture;
imgBild.Source = FileToBitmapImage(dlg.FileName); // Bild aus Filesystem laden und anzeigen
context.Pictures.Add(picture);
pictures.Add(picture);
ind = pictures.Count - 1; // Index für den neuen Datensatz ist pictures.Count - 1
...
private BitmapImage FileToBitmapImage(string source)
{
try
{
if (source == string.Empty) { return null; }
BitmapImage bitmapimage = new BitmapImage();
using (FileStream filestream = File.Open(source, FileMode.Open))
{
bitmapimage.BeginInit();
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.StreamSource = filestream;
bitmapimage.EndInit();
return bitmapimage;
} // using
} // try
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Nachricht", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
} // catch
} // FileToBitmapImage
Code of Converter:
class PhotoConverter : IValueConverter
{
// Convert (Quelle -> Ziel)
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
if (value == null) { return null; }
byte[] byteArray = (byte[])value;
using (MemoryStream memorystream = new MemoryStream(byteArray))
{
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.StreamSource = memorystream;
bitmapimage.EndInit();
return bitmapimage;
} // using
} // try
catch (Exception)
{
return null;
} // catch
} // Convert
// ConvertBack (Ziel -> Quelle)
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
if (value == null) { return null; }
ImageSource imagesource = (ImageSource)value;
BitmapSource bitmapsource = imagesource as BitmapSource;
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapsource));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
byte[] bytearray = ms.ToArray();
return bytearray;
} // using
} // try
catch (Exception)
{
return null;
} // catch
} // ConvertBack
} // PhotoConverter