Bas*_*ili 17 entity-framework-core
我需要Entity Framework Core的任何NoSQL提供程序.我可以将EF-Core版本与MongoDB/Raven或其他任何东西一起使用吗?
Mor*_*avi 23
对Azure Table存储,Redis和其他(如MongoDb)等NoSQL数据库提供商的支持仍然存在于EF Core团队积压中,尚未实施,并且不会针对Core 1.0.0版本实施.
也就是说,根据EF核心路线图,对NoSQL数据库提供商的支持是团队的一项高优先级功能,并将在Core 1.0.0发布后的未来版本中发布.
(移动评论回答,所以我没有劫持@ MortezaManavi的回答)
在您的问题中,您引用EF Core.正如我所提到的,我们有许多NoSQL数据源的ADO.NET提供程序.您可以为我们的任何提供商下载30天免费试用版(或公开测试版,具体取决于数据源).我在答案的底部包含了我们当前NoSQL产品的链接.
我们在知识库中有一篇文章,使用代码优先方法使用EF6连接到MongoDB数据(尽管可以应用这些原则而不管数据源).我在这里转录了那篇文章的内容.
修改项目中的App.config文件以添加对MongoDB Entity Framework 6程序集和连接字符串的引用.
设置Server,Database,User和Password连接属性以连接到MongoDB.
<configuration>
...
<connectionStrings>
<add name="MongoDBContext" connectionString="Offline=False;Server=MyServer;Port=27017;Database=test;User=test;" providerName="System.Data.CData.MongoDB" />
</connectionStrings>
<entityFramework>
<providers>
...
<provider invariantName="System.Data.CData.MongoDB" type="System.Data.CData.MongoDB.MongoDBProviderServices, System.Data.CData.MongoDB.Entities.EF6" />
</providers>
<entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)添加对System.Data.CData.MongoDB.Entities.EF6.dll的引用,该文件位于安装目录的lib - > 4.0子文件夹中.
将新的.cs文件添加到项目中并向其中添加一个类.这将是您的数据库上下文,它将扩展DbContext类.在该示例中,此类名为MongoDBContext.以下代码示例重写OnModelCreating方法以进行以下更改:
删除对MigrationHistory表的请求.
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class MongoDBContext : DbContext {
public MongoDBContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// To remove the requests to the Migration History table
Database.SetInitializer<MongoDBContext>(null);
// To remove the plural names
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Run Code Online (Sandbox Code Playgroud)创建另一个.cs文件,并在要检索的MongoDB实体之后将其命名,例如Customers.在此文件中,定义实体和实体配置,它们将类似于以下示例:
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
[System.ComponentModel.DataAnnotations.Schema.Table("Customers")]
public class Customers {
[System.ComponentModel.DataAnnotations.Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public System.String _id { get; set; }
public System.String CompanyName { get; set; }
}
public class CustomersMap : EntityTypeConfiguration<Customers> {
public CustomersMap() {
this.ToTable("Customers");
this.HasKey(Customers => Customers._id);
this.Property(Customers => Customers.CompanyName);
}
}
Run Code Online (Sandbox Code Playgroud)现在您已经创建了一个实体,将该实体添加到您的上下文类中:
public DbSet<Customers> Customers { set; get; }
Run Code Online (Sandbox Code Playgroud)完成上下文和实体后,您现在可以在单独的类中查询数据了.例如:
MongoDBContext context = new MongoDBContext();
context.Configuration.UseDatabaseNullSemantics = true;
var query = from line in context.Customers select line;
Run Code Online (Sandbox Code Playgroud)小智 6
免责声明:我是该开源项目的所有者和运营商。
如果您仍在寻找MongoDB EF-Core提供程序,则可以在GitHub上找到我的提供程序:EntityFrameworkCore.MongoDB。该项目当前包括EF-Core数据库提供程序和ASP.NET Core身份提供程序。
注意:提供程序仍在预览/预发布中,等待对EF-Core的复杂类型的适当支持StateManager。
您可以通过将以下NuGet源添加到项目中来获取软件包:
nuget sources add -name EFCore-MongoDb -Source https://www.myget.org/gallery/efcore-mongodb
查看入门Wiki,以进行更仔细的了解。
| 归档时间: |
|
| 查看次数: |
26339 次 |
| 最近记录: |