Skip to content

cblx/efcore.dataverse

Repository files navigation

Cblx.EntityFrameworkCore.Dataverse

Extends SqlServer provider for Dataverse

NuGet Version NuGet Downloads


public class DynamicsContext(DbContextOptions<DynamicsContext> options) : DataverseDbContext(options){
    ...
}
services.AddDbContext<DynamicsContext>(options => options.UseDataverse(dataverse =>
  dataverse.ClientId(configuration["ClientId"])
           .ClientSecret(configuration["ClientSecret"])
           .ResourceUrl(configuration["ResourceUrl"])
           .Authority(configuration["Authority"])
);

Model Configuration

For saving data, this libs needs to know more information about the entities, like the entity set name and the Dataverse navigation property name for relationships.

ToEntitySet

This library overrides SaveChangesAsync and perform a request to $batch Dataverse API endpoint for saving data. To do so, it needs to know the entity set for write operations.

entityBuilder.ToTable("account");
entityBuilder.ToEntitySet("accounts");

HasODataBindPropertyName

When the entity has a relationship, the lib needs to know what's the navigation name to update the FK property.

Desired write body:

{
   "other_entity@odata.bind": "other_entities(<guid>)",
   "etc": "other values"
}

Configuring relationship

entityBuilder.Property(x => x.OtherEntityId)
             .HasColumnName("other_entity")
             .HasODataBindPropertyName("other_entity");
// The lib will infer the entity set configured for the other entity
entityBuilder.HasOne(x => x.OtherEntity).WithMany().HasForeignKey(x => x.OtherEntityId);

HasForeignEntitySet

This is used when the FK has no correspondent Entity defined in the model:

entityBuilder.Property(x => x.OtherEntityId)
             .HasColumnName("other_entity")
             .HasODataBindPropertyName("other_entity")
             .HasForeignEntitySet("other_entities");

Many-to-many Dataverse relationship

   builder.HasMany(e => e.Others)
       .WithMany()
       .UsingDataverseManyToManyRelationshipEntity(
           joinTable: "account_other", 
           rightForeignKey: "otherid", 
           leftForeignKey: "accountid", 
           navigationFromLeft: "account_others");

GetOptionsAsync

Gettinge choice options for a property

db.Entities.GetOptionsAsync(e => e.ChoiceProperty);

About

Extends EF Core SqlServer provider for Dataverse

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages