Azure Data Explorer C# querying and ORM - entity-framework

I looked into querying data from DotNet and found: https://github.com/Azure/azure-kusto-samples-dotnet samples.
I see no ORM examples there and this got me to try something myself
public class KustoContext : DbContext
{
public KustoContext(KustoConfiguration kustoConfiguration)
: base(new KustoConnectionStringBuilder(kustoConfiguration.ClusterUri, kustoConfiguration.DatabaseName)
.WithAadApplicationKeyAuthentication(kustoConfiguration.ClientId, kustoConfiguration.ClientSecret, kustoConfiguration.TenantId)
.ConnectionString)
{
}
public DbSet<SomeEntity> SomeEntities{ get; set; }
}
Unfortunately, I get an error:
Exception thrown: 'System.ArgumentException' in System.Data.dll
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll
Keyword not supported: 'application client id'.
This makes sense, I guess I need a provider for adx, but I couldn't find a NuGet package for that.
Ideally, I would like to use Entity Framework with Azure Data Explorer. Is this possible? Are there any available samples?
UPDATE: are there any alternatives for Entity Framework?

Ideally, I would like to use Entity Framework with Azure Data Explorer. Is this possible? Are there any available samples?
I don't believe anyone has written an Entity Framework provider for Azure Data Explorer.

Related

Using interfaces in dependency injection in .Net MAUI

I am trying to implement dependency injection for the first time in my new project in .Net MAUI. For testing purposes, I want to fetch data from a local source in my ViewModel. The production scenario will fetch data from the remote data source using HttpClient.
Below is my code base structure:
I have an Interface:
public interface IApiService
{
Task<bool> GetSomething(string parameter);
Task<string> GetSomethingElse(string parameter);
}
I have two classes that derive from it.
public class LocalDataStore: IApiService
public class RemoteDataStore: IApiService
In my MauiProgram.cs, when I want to use Local Data Store:
builder.Services.AddSingleton<LocalDataStore>()
builder.Services.AddSingleton<IApiService>()
And for Remote Data Store
builder.Services.AddSingleton<RemoteDataStore>()
builder.Services.AddSingleton<IApiService>()
In my ViewModel:
public class Page1ViewModel
{
public Page1ViewModel(IApiService localDataStore)
{
var items = Task.Run(async () => await localDataStore.GetSomething(true));
}
}
While running the app, I get an error :
System.InvalidOperationException: 'Unable to resolve service for type '...IApiService' while attempting to activate 'ViewModels.Page1ViewModel'.'
What am I doing wrong or what else should I be doing?
Kindly help.
Thanks, and regards.
Edit*
Of course, it works if I use LocalDataStore or RemoteDataStore instead of IApiService, when I register the services with the builder. But then if I have to change from one data store to another, I will have to change that in all the ViewModel classes?
It was trivial.
I needed to register the service like so :
builder.Services.AddSingleton<IApiService,LocalDataStore>();
Thanks to https://youtu.be/paZNvvUNFi0, I realised that.
Hope this helps someone.

Azure Mobile Apps - Entity Framework - Foreign Key and other Attributes missing

In Azure Mobile Apps I'm trying to add a foreign key attribute to my DataObject class.
It seems like any attribute is not recognized, and I get an error msg..
using Microsoft.Azure.Mobile.Server;
using System.ComponentModel.DataAnnotations.Schema;
namespace SomeNameSpace.DataObjects
{
[ForeignKey("examplekeyname")]
public string SomeDataColmnName {get; set;}
I have done this before so I'm confused as to what is causing this.
Went through several Azure Mobile Apps Tutorials again but doesn't seem to be working.
I get the following error msg:
Error CS0246
The type or namespace name 'ForeignKeyAttribute' could not be found
(are you missing a using directive or an assembly reference?)
It's not just foreign key.. other attributes show the same behavior... like [Required]
Those attributes are in the assembly System.ComponentModel.DataAnnotations. See ForeignKeyAttribute Class on MSDN.

MVC FoolProof Validation showing exception while calling SaveChange method

This question mayt sound duplicate but I am not satisfied with any of the answers as some are suggesting MVC Foolproof validaiton for conditional validaiton and some tells it dont work well with entity framework
I am using MVC Foolproof RequiredIf validation in my project.It works well on clientside and is validation is working on server side as well.
[RequiredIf("STCompulsory",Operator.EqualTo,true,ErrorMessage="Please enter Registration No")]
public string STRegNo { get; set; }
But when i call db.Savechanges() to insert data an exception is coming
An unexpected exception was thrown during validation of 'STRegNo' when invoking
Foolproof.RequiredIfAttribute.IsValid. See the inner exception for details.
InnerException
The method or operation is not implemented.
You do not need the Operator.EqualTo parameter and it can be simply
[RequiredIf("STCompulsory", true, ErrorMessage="Please enter Registration No")
public string STRegNo { get; set; }
You are correct in that the foolproof [RequiredIf] attribute does have some problems with EF and it is discussed in detail in this work issue (along with some suggested changes).
The easiest solution is to use a view model rather than your data model in the view, and apply the attribute to the view model property.

ProviderIncompatibleException with Entity Framework

I always get this error when I issue this command
http://localhost:19572/api/courses
This method is generating the exception
public IQueryable GetAllCourses()
{
return _ctx.Courses
.Include(“CourseSubject”)
.Include(“CourseTutor”)
.AsQueryable();
}
Exception:
System.Data.Entity.CoreProviderIncompatibleExceptionoccurred inEntityFramework.dll` but was not handled in user code
Additional information An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
and when I inspect the JSON element in Fiddler I always get this
ExceptionMessage=The system cannot find the file specified
ExceptionType=System.ComponentModel.Win32Exception
Message=An error has occurred.
I am using Entity Framework 6.0.1, and I have tried 5 but it didn’t work so I there any thing wrong with Entity Framework version or what?

"Entity does not have a key defined" error in working production code

We are running into a very odd problem: We are using entity framework 4 and ria services for a silverlight 5 appliation hosted in IIS 6.1. For long periods of time, everything is running smoothly, but occasionally the application fails with the following error message in the event log:
WebHost failed to process a request. Sender Information:
System.ServiceModel.Activation.HostedHttpRequestAsyncResult/56703158
Exception: System.ServiceModel.ServiceActivationException: The service
'/Services/EcoFleet-DomainServices-Repository-EcofleetRepository.svc'
cannot be activated due to an exception during compilation. The
exception message is: The Entity 'DeviceData' in DomainService
'EcofleetRepository' does not have a key defined. Entity types exposed
by DomainService operations must have at least one public property
marked with the KeyAttribute.. ---> System.InvalidOperationException:
The Entity 'DeviceData' in DomainService 'EcofleetRepository' does not
have a key defined. Entity types exposed by DomainService operations
must have at least one public property marked with the
KeyAttribute. at
System.ServiceModel.DomainServices.Server.DomainServiceDescription.ValidateEntityTypes()
at
System.ServiceModel.DomainServices.Server.DomainServiceDescription.Initialize()
at
System.ServiceModel.DomainServices.Server.DomainServiceDescription.CreateDescription(Type
domainServiceType) at
System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey
key, Func2 valueFactory) at
System.ServiceModel.DomainServices.Server.DomainServiceDescription.GetDescription(Type
domainServiceType) at
System.ServiceModel.DomainServices.Hosting.DomainServiceHost..ctor(Type
domainServiceType, Uri[] baseAddresses) at
System.ServiceModel.DomainServices.Hosting.DomainServiceHostFactory.CreateServiceHost(Type
serviceType, Uri[] baseAddresses) at
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String
constructorString, Uri[] baseAddresses) at
System.ServiceModel.ServiceHostingEnvironment.HostingManager.CreateService(String
normalizedVirtualPath, EventTraceActivity eventTraceActivity) at
System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(ServiceActivationInfo
serviceActivationInfo, EventTraceActivity eventTraceActivity)
at
System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String
normalizedVirtualPath, EventTraceActivity eventTraceActivity)
--- End of inner exception stack trace --- at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult
result) Process Name: w3wp Process ID: 2300
The application still responds, but no entities can be loaded from the server. After stopping and starting the site, everything works again.
The entity in this case (DeviceData) is a database table, and it does have a key, and regardless it seems to be random which entity fails. Furthermore I would only expect to see the "key not defined" error at compile time. Does anyone have a clue what the problem is, and how to fix it?
Your DeviceData entity do not have key defined, which is required. You should create property with name Id or DeviceDataId which will be treated as key by convention (Entity Framework currently looks for these properties). Also you can use mappings to set other property as key. This can be achieved with data annotations attributes:
[Key]
public int Foo { get; set; }
Or with fluent mappings:
modelBuilder.Entity<DeviceData>().HasKey(d => d.Foo);
First you won't have an error at compile time because it is not a mandatory thing, but not having one might give you quite the headache, because EF use it in many ways. so you have to redefine your key by naming it Id or DeviceDataId(Using the convention) or define a one from the model.
It seems from your question that there is a key defined for DeviceData, in that case I advice you to relook the properties of the key column in your model.
remember also that not having a key column might reduce the performance of your app.