Extends SqlServer provider for Dataverse
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"])
);
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.
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");
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);
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");
builder.HasMany(e => e.Others)
.WithMany()
.UsingDataverseManyToManyRelationshipEntity(
joinTable: "account_other",
rightForeignKey: "otherid",
leftForeignKey: "accountid",
navigationFromLeft: "account_others");
Gettinge choice options for a property
db.Entities.GetOptionsAsync(e => e.ChoiceProperty);