Как использовать .sdf файлы в .NET Core - PullRequest
2 голосов
/ 25 октября 2019

В .NET Framework мы можем загрузить файл .sdf , используя System.Data.Linq.DataContext. Но это недоступно в .NET Core

public partial class Northwind : System.Data.Linq.DataContext
{
   private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
   ....
   public Northwind(string connection) :
      base(connection, mappingSource)
   {
      OnCreated();
   }

 .....
 public System.Data.Linq.Table<Categories> Categories 
 {
    get
    {
       return this.GetTable<Categories>();
    }
 }

 public System.Data.Linq.Table<Customers> Customers
 {
    get
    {
       return this.GetTable<Customers>();
    }
 }

 public System.Data.Linq.Table<Employees> Employees
 {
    get
    {
       return this.GetTable<Employees>();
    }
 }

 public System.Data.Linq.Table<OrderDetails> OrderDetails
 {
    get
    {
       return this.GetTable<OrderDetails>();
    }
 }

 public System.Data.Linq.Table<Orders> Orders
 {
    get
    {
       return this.GetTable<Orders>();
    }
 }

 public System.Data.Linq.Table<Products> Products
 {
    get
    {
       return this.GetTable<Products>();
    }
 }
}


// Getting Data
private void PopulateData()
{
  Random r = new Random();

  Northwind north = new Northwind(string.Format(@"Data Source= {0}", 
  FindFile("Northwind.sdf")));

  foreach (OrderDetails orderDet in north.OrderDetails.Take(50))
  {
      OrderInfo orderInfo = new OrderInfo();
      orderInfo.OrderID = orderDet.OrderID;
      orderInfo.CustomerID = orderDet.Orders.CustomerID;
      orderInfo.ProductName = orderDet.Products.ProductName;
      orderInfo.UnitPrice = (double)orderDet.UnitPrice;
      orderInfo.OrderDate = (DateTime)orderDet.Orders.OrderDate;
      orderInfo.DeliveryDelay = (DateTime)orderDet.Orders.ShippedDate - 
orderInfo.OrderDate;
      orderInfo.Quantity = orderDet.Quantity;

      orderInfo.ContactNumber = r.Next(999111234, 999111239).ToString();

      orderInfo.ShipAddress = orderDet.Orders.ShipAddress;

      _orderList.Add(orderInfo);
  }
}

Данные для приложения будут извлекаться с использованием DataContext в приложении .NET Framework, теперь в .NET Core мы не могли работать сзагрузка .sdf файлов.

Как мы можем использовать .sdf файлов в .NET Core?

1 Ответ

1 голос
/ 25 октября 2019

Вы можете использовать поставщика ADO.NET напрямую, но только в Windows.

Некоторые примеры кода и советы здесь: https://github.com/dotnet/corefx/issues/33897#issuecomment-536269132

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...