DbSet declaration format - dbset

I originally developed a database(with UI) to add new records, retrieve list based on a query, and allow data alterations.
The two tables I used have now been incorporated into a SQL Server database, and I need to duplicate the same user experience in the new environment. I hope I am almost successful, but I still have errors in 2 files.
Within the ApplicationDbContext.cs file,
(1) I attempted to use the class name in declaring the DbSet statements. I get the following error in each instance:
"The type or namespace name '(className)' could not be found (are you missing a using directive or an assembly reference?)
(Should I be using something besides the class name in the DbSet statement?)
(2) I declare ApplicationDbContext as follows:
`public ApplicationDbContext() : base(ApplicationDbContext)
{}
The following error appears:
"'ApplicationDbContext' is a type, which is not valid in the given context"
In Startup.cs, ConfigureServices looks like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<ApplicationDbContext>(options =>
{
UseSqlServer(Configuration.GetConnectionString("AFForm1067DbConnection" ));
});
services.AddRazorPages();
//services.AddSingleton<IAFForm1067Repository,
MockAFForm1067Repository>();
}
I get the following error:
"The name 'UseSqlServer' does not exist in the current context"
Any help would be very much appreciated!

Related

hotchocolate throws error when using UseFiltering() on a field

I have a pretty simple setyp where I'm putting graphql over an entityframework datacontext (sql server).
I'm trying to get filtering to work. I've tried adding .UseFiltering() to a field descriptor like so...
descriptor.Field(t => t.AccountName).Type<NonNullType<StringType>>().UseFiltering();
But it causes this error on startup...
HotChocolate.SchemaException: 'Unable to infer or resolve a schema
type from the type reference Input: System.Char.'
I assume I'm doing something wrong somewhere...
"UseFiltering" is supposed to be used to filter data which represents a collection of items in some way (IQueryable, IEnumerable, etc).
For instance, if you have users collection and each user has AccountName property you could filter that collection by AccountName:
[ExtendObjectType(Name = "Query")]
public class UserQuery
{
[UseFiltering]
public async Task<IEnumerable<User>> GetUsers([Service]usersRepo)
{
IQueryable<User> users = usersRepo.GetUsersQueryable();
}
}
In that example the HotChocolate implementation of filtering will generate a number of filters by user fields which you can use in the following way:
users(where: {AND: [{accountName_starts_with: "Tech"}, {accountName_not_ends_with: "Test"}]})
According to your example: the system thinks that AccountName is a collection, so tries to build filtering across the chars the AccountName consists of.

Hot Chocolate - Is it possible to implement my own object type with generics?

I wrote the following object type class.
public class ResponseType<T> : ObjectType<ResponseEntry<T>>
{
protected override void Configure(IObjectTypeDescriptor<ResponseEntry<T>> descriptor)
{
descriptor.Name("Response");
}
}
I want to use it like this as the outermost type in the resolver definition.
descriptor.Field<SharedResolvers>(r => r.GetObject1(default, default, default, default))
.Type<ResponseType<ListType<Object1>>>()
.Name("object1");
descriptor.Field<SharedResolvers>(r => r.GetObject2(default, default, default, default))
.Type<ResponseType<ListType<Object2>>>()
.Name("object2");
This code works if I only implement object1 however as soon as I add object2 I get the following error.
System.Collections.Generic.KeyNotFoundException: 'The given key 'HotChocolate.Configuration.RegisteredType' was not present in the dictionary.'
It seems as though there may be some issue with declaring two resolvers of the same class type. Is that the case? And if so, what are my options?
I was able to resolve the issue by setting the descriptor.Name to a unique value based on T.
descriptor.Name($"Response_{typeof(T).GetHashCode()}");
Then I realized my real issue was that I was defining the name at all. If you don't override the name it automatically comes up with a unique name/key based on the type definition.

Error on GraphQL : no provided id but the store already contains an id of

I am very new to GraphQL and Apollo, and I don't understand what is wrong with the 'aliasEmail' field below. When I add this one to the query, I get this error message. When I remove it from the query, everything works perfectly.
It is well defined in 'types.graphql' and is just a simple string field.
index.js:2178 Unhandled (in react apollo:Apollo(withRouter(EmailSettingsContainer))) Error: Network
error: Error writing result to store for query:
query getCompanyForAliasEmailEditForm($companyId: ID) {
Company(id: $companyId) {
name
isTaxActive
telFixe
aliasEmail
telMobile
__typename
}
}
Store error: the application attempted to write an object with no
provided id but the store already contains an id of
Company:cje6xkcnxl83u01353a20p1t6 for this object. The selectionSet
that was trying to be written is:
Company(id: $companyId) {
name
isTaxActive
telFixe
aliasEmail
telMobile
__typename
}
It sounds like there's already another query being made elsewhere in the app that also returns the Company object and that query includes the id (or _id) field, while the query in this case does not. Apollo uses both the typename and id to generate the cache key for your query result (unless dataIdFromObject is used to modify this behavior), and so throws an error when it detects the above discrepancy. Try including the id field in your getCompanyForAliasEmailEditForm query.

Breeze - expand results in Object #<Object> has no method 'getProperty' Query failed

In my edmx model are 2 related tables: Challenge and ChallengeNote (has FK back to ChallengeID)
I can do this in breeze all day long
var qry = dataservice.getQuery("Challenges");
However, this fails every time:
var qry = dataservice.getQuery("Challenges").expand("ChallengeNotes");
The searchFailed is called and is the only error information in the console.
return dataservice.execute(qry.inlineCount(true))
.then(seachSucceeded)
.fail(searchFailed);
Does Breeze support relational data like this?
Does one need to write some custom code to support?
What am I missing?
Here's related answered question, but I was already following (unless I missed something) the answer's solution (and why I have the 2 context.Configuration settings in my ContextProvider).
breezejs-error-when-loading-an-entity-with-related-data
Here's another similar question that's been unanswered breeze-expand-query-fails-with-object-object-has-no-method-getproperty
Here's my provider code (want to use the BeforeSaveEntity override further on in the project):
public class ModelProvider : EFContextProvider<ModelEntities>
{
public ModelProvider()
: base()
{
this.Context.Configuration.LazyLoadingEnabled = false;
this.Context.Configuration.ProxyCreationEnabled = false;
}
}
Here's my controller code:
[BreezeController]
public class DataController : ApiController
{
readonly ModelProvider _contextProvider = new ModelProvider();
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
public IQueryable<Challenge> Challenges()
{
return _contextProvider.Context.Challenges.Include(x => x.ChallengeNotes);
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
[HttpGet]
public IQueryable<ChallengeNote> ChallengeNotes()
{
return _contextProvider.Context.ChallengeNotes;
}
}
When I browse to the URL, it's including the related entity:
http://localhost:53644/breeze/data/Challenges?$filter=Active%20eq%20true&$top=10&$expand=ChallengeNotes&$inlinecount=allpages
Here is the data coming from the Controller
At this point all things, imo, are pointing to Breeze configuration on either the Server or Client.
TIA
Breeze absolutely does support this, but you do need to make sure that your Entity Framework model is set up correctly. Take a look at the DocCode sample in the Breeze zip for a number of examples of using both expand (client side) or EF include (server side) clauses.
One guess about your problem is that you are using the Breeze camelCasing naming convention and therefore your "expand" clause should be
var qry = dataservice.getQuery("Challenges").expand("challengeNotes");
i.e. "challengeNotes" (note the casing) is the name of the client side property that corresponds to a server side property of "ChallengeNotes". To clarify, "expand" clauses take the names of client side "properties" as parameters and property names are what are transformed as a result of the Breeze.NamingConvention.
In contrast, a query resource name i.e. "Challenges" in your example is the name of the server side resource ( as a result of marking your "Challenges" method with the [HttpGet] annotation. This name is NOT affected by the NamingConvention.
Side notes: Your example has both an expand and an Include clause. Either of these is sufficient all by itself. You do not need both. In general you can either include an "expand" clause in your client side query OR have an Entity Framework "Include" clause on the server. The advantage of the first is that you can control the expand on the client, the advantage of the second is that you can insure that every query for a specified resource always fetches some related entities.
Hope this helps!

MVC ExecuteQuery not a valid method on Context object

I am trying to execute a sql command directly against the database. However, intellisense does not see ExecuteQuery as a valid method against my context variable. I am sure I am missing something obvious.
My context class:
public class CatastropheContext : DbContext
{
public DbSet<CLIENT> CLIENTs { get; set; }
...
}
My attempt to establish the query:
CatastropheContext db = new CatastropheContext();
IEnumerable<ClientClaim> = db.ExecuteQuery
In the code above, ExecuteQuery is flagged as invalid an intellisense suggests creating a stub method.
Can you use Database.ExecuteSqlCommand where Database comes from the DbContext class.
This seems to me like you are missing some references. Make sure you are:
using System.Data.Linq;
Here is the MSDN reference on ExecuteQuery. Notice the namespace.

Resources