如何在.NET Core中使用.sdf文件



在.NET框架中,我们可以使用System.Data.Linq.DataContext加载.sdf文件。但这在.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);
}
}

应用程序的数据将在.NET Framework应用程序中使用DataContext检索,现在在.NET Core中,我们无法加载.sdf文件。

如何在.NET Core中使用.sdf文件

您可以直接使用ADO.NET提供程序,但只能在Windows上使用。

这里有一些示例代码和提示:https://github.com/dotnet/corefx/issues/33897#issuecomment-536269132

最新更新