How to query using TableQuery via IEnumerable? - c#

I am exposing my Table via the following method:
public static class TableCacheService
{
public static IEnumerable<Myentity> Read()
{
var acc = new CloudStorageAccount(
new StorageCredentials("account", "mypass"), false);
var tableClient = acc.CreateCloudTableClient();
var table = tableClient.GetTableReference("Myentity");
return table.ExecuteQuery(new TableQuery<Myentity>());
}
}
Here's a usage example:
var nodeHasValue = TableCacheService.Read()
.Where(x => note.ToLower().Contains(x.RowKey.ToLower()))
.Any();
But supposedly x.RowKey is null!
It looks like there are 2 members both called RowKey. One of them is a TableEntity:
How do I access the RowKey that is non-null? Am I incorrectly querying the table?

It seems you have hiding properties RowKey and PartitionKey inside your Myentity class.
I guess your Myentity class has something like it:
public class Myentity: TableEntity
{
public new string RowKey { get; set; }
...
}
Pay attention to keyword new.
If you have this construction you need to delete this property.
More information about hiding and overriding you can find here.
Also please review work example with ATS (Azure Table Storage) is here.

Related

Entity Framework ObjectQuery.Include()

I have an object with two objects as properties (User, PrimaryNode), both could potentially be null, see below:
public class Item
{
[Key]
public int ItemId { get; set; }
public string ItemName { get; set; }
public Node PrimaryNode { get; set; }
public User User { get; set; }
}
I'm using Entity Framework 6 to populate the Item object and using chained includes to populate the PrimaryNode and User objects within it.
When the first chained Include has a null object then the whole object returns as null, for example:
using (var db = new MyContext())
{
var item = db.Items.Include(i => i.User).Include(n => n.PrimaryNode).FirstOrDefault(i => i.ItemId == id);
}
If in the above example i.User is null then the item variable is null. Whats the best way of populating both the sub-objects in a way that if a sub-object is null then the parent object and the other sub-object will still be populated?
I don't think your issue is due to the Include calls. According with the documentation:
This extension method calls the Include(String) method of the
IQueryable source object, if such a method exists. If the source
IQueryable does not have a matching method, then this method does
nothing.
In other words is going to be translated to:
var item = db.Items.Include("User").Include("PrimaryNode").FirstOrDefault(i => i.ItemId == id);
My question is, are you sure you have an Item with that id properly related with existing rows in Users and PrimaryNodes tables in your DB?. When you call Include method at the end is going to be translated to a join, so if the FK of your relationship doesn't match with the PK that reference, your query should not return what you are expecting.
Anyways, if you want to try another variant to load related properties you can use Explicit Loading:
var item = db.Items.FirstOrDefault(i => i.ItemId == id);
context.Entry(item).Reference(p => p.PrimaryNode).Load();
context.Entry(item).Reference(p => p.User).Load();
I think it would be better if you use Lazy loading int his situation. Just make the User and PrimaryNode virtual:
public class Item
{
[Key]
public int ItemId { get; set; }
public string ItemName { get; set; }
public virtual Node PrimaryNode { get; set; }
public virtual User User { get; set; }
}
And then:
var db = new MyContext();
var item = db.Items.FirstOrDefault(i => i.ItemId == id);
As others have mentioned, I think your issue is not due to the Includes. However, I think the following method has value. It is functionally equivalent to what you are already doing with the chained includes, but I think it has several benefits including making the intention of the code clear to the user.
The includes can be placed in Extension methods:
using System.Data.Entity;
using System.Linq;
namespace Stackoverflow
{
public static class EntityExtensions
{
public static IQueryable<Item> IncludePrimaryNode(this IQueryable<Item> query)
{
// eager loading if this extension method is used
return query.Include(item => item.PrimaryNode);
}
public static IQueryable<Item> IncludeUser(this IQueryable<Item> query)
{
// eager loading if this extension method is used
return query.Include(item => item.User);
}
}
}
Then, you can use the extensions as follows:
using (var db = new MyContext())
{
var itemQuery = db.Items.IncludeUser();
itemQuery = itemQuery.IncludePrimaryNode();
var item = itemQuery.FirstOrDefault(i => i.Id == 1);
}
It's just another way of doing the same thing, but I like the clarity it adds to the code.

Azure Document DB UpdateDoc

I am starting off with azure document db. I was trying to update an existing document. When I use the following query everything works:
dynamic Team2Doc = client.CreateDocumentQuery<Document>(documentCollection.DocumentsLink).Where(d => d.Id == "t002").AsEnumerable().FirstOrDefault();
Team2Doc.TeamName = "UPDATED_TEAM_2";
await client.ReplaceDocumentAsync(Team2Doc);
but if use the below code:
dynamic Team2Doc = client.CreateDocumentQuery<Document>(documentCollection.DocumentsLink).Where(d => d.TeamName== "team1").AsEnumerable().FirstOrDefault();
Team2Doc.TeamName = "UPDATED_TEAM_2";
await client.ReplaceDocumentAsync(Team2Doc);
I get this error:
"The best overloaded method match for
'Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentAsync(Microsoft.Azure.Documents.Document,
Microsoft.Azure.Documents.Client.RequestOptions)' has some invalid
arguments"
Is there anyway to retrieve a document by one of the properties and update the document?
The where clause is trying to query the property TeamName which does not exist in Document class.
Changing the type of the queryable to your data model should fix it.
For example, say you have the following data model:
public class EmployeeDocument : Document
{
// Other properties that you may have similarly defined ....
public class string TeamName
{
get
{
return this.GetValue<string>("TeamName");
}
set
{
this.SetValue("TeamName", value);
}
}
}
Then you can modify your query like this:
var team2Doc = client.CreateDocumentQuery<EmployeeDocument>(documentCollection.DocumentsLink).Where(d => d.TeamName== "team1").AsEnumerable().FirstOrDefault();
team2Doc.TeamName = "UPDATED_TEAM_2";
await client.ReplaceDocumentAsync(team2Doc);
Note that you have to use the EmployeeDocument, instead of the Document class, while creating the document queryable. That will let you query on EmployeeDocument properties.
SQL Version
Creating a document model for each of your existing data models may not be feasible if you have a large number of data models. In that case you may want to try out the SQL query syntax.
Refer to Aravind's answer in this post. The example he uses is for deleting documents, but it can be easily modified to update them too.
You can also create a model with Id:
public class Employee
{
[JsonPropery("id")]
public class string Id { get; set; }
public class string TeamName { get; set; }
}
And then replace the document using it's Id:
var employee= client
.CreateDocumentQuery<Employee>(documentCollection.DocumentsLink)
.Where(d => d.TeamName== "team1")
.AsEnumerable()
.FirstOrDefault();
employee.TeamName = "team2";
var documentUri = UriFactory.CreateDocumentUri(databaseName, collectionName, employee.Id);
await client.ReplaceDocumentAsync(employee);

MongoDb serialize generic types (classes)

What is the general advice on serializing something like this in MongoDb
public class BaseCategorization<T> where T : BaseCategory
{
public BaseCategorization()
{
Type = typeof(T).Name;
}
public BaseCategorization(int id)
{
Id = id;
Type = typeof(T).Name;
}
...
}
I use it like this:
_documentsProvider.Save<BaseCategorization<ProductCategory>>(newProductCategorization);
where ProductCategory : BaseCategory and BaseCategory is abstract.
The above seems to work, only problem is the strange name of the resulting collection, BaseCategorization'1, and the fact that I don't know whether this design is fine by MongoDb terms.
You certainly can store all of your documents in one collection, however it is probably easier and cleaner in the long run to only store one type in a collection (from a C# perspective). You can do this by creating a class with an Extension Method on MongoDatabase.
public static class MyDatabase
{
public static MongoCollection<T> GetCollection<T>(this MongoDatabase db)
{
var name = typeof(T).Name;
return db.GetCollection<T>(name);
}
}
Then you can just call GetCollection with your type on it and the name will be hidden from your general code. You can also create something like this to abstract things a little bit further:
public class MyDatabase
{
private MongoDatabase _db;
public MyDatabase(MongoDatabase db)
{
_db = db;
}
public MongoCollection<object> Objects
{
get
{
return _db.GetCollection<object>();
}
}
}
With this you can just do
var client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("MyDb");
var myDb = new MyDatabase(db);
myDb.Objects.Find();
This Find will map to execute against the Objects collection, simply replace <object> with your type and give the collection a name.

Custom key generation in RavenDB

I have sets of entities all of them are derived from abstract class
public abstract class NamedEntity : INamedEntity
{
#region Public Properties
public string Description { get; set; }
public string Id { get; set; }
public string Name { get; set; }
#endregion
}
When I persist all entities I want to use Name field as a key, so I override DocumentKeyGenerator and provide such implementation:
store.Conventions.DocumentKeyGenerator = entity =>
{
var namedEntity = entity as NamedEntity;
if (namedEntity != null)
{
return string.Format("{0}/{1}", store.Conventions.GetTypeTagName(entity.GetType()), namedEntity.Name);
}
return string.Format("{0}/", store.Conventions.GetTypeTagName(entity.GetType()));
};
It works fine when I persist the list of entities for the first time, but if I want to persist them again I get an exception
PUT attempted on document 'xxxxx' using a non current etag
I just started using RavenDB, so I cannot understand what I am doing wrong?
Just a guess, but it's probably not with your key generation, but how you are storing them.
On first usage you probably have something like:
var myEntity = new MyEntity(...);
session.Store(myEntity);
...
session.SaveChanges();
That part is fine, but on subsequent usage, you should not be doing the same thing. Instead, it should be more like this:
var myEntity = session.Load<MyEntity>("myentities/foobar");
myEntity.Something = 123;
...
session.SaveChanges();
Note there is no call to .Store() when making changes. This is because the entity is "tracked" by the session, and all changes to it are automatically persisted when you call .SaveChanges()

ASP.NET C# Entity Framework - How to update Foreign Key properly? - Part 2

This question is like a Part 2 of my previous one here!
Here are things I learned and hoping to be true:
Do not use Data Entity Context as Static
Dispose the data context after used -usually after one time-
Pass parameters into UPDATING Entity method instead of the Entity
(which I'll explain and in here my crossroad lies)
Here is my modified code:
public class ManagerBase
{
private NoxonEntities _entities = null;
public NoxonEntities Entities
{
get
{
if (_entities == null)
_entities = new NoxonEntities();
return _entities;
}
}
}
//Managers uses ManagerBase class as a Base class
MemberManager currentMemberManager = new MemberManager();
currentMemberManager.Save(memberId, username, password, languageId);
//MemberManager class
public Member Save(long memId, string username, string password, int langId)
{
using (var Context = base.Entities)
{
var Data = Context.Member.First(c => c.Id == memId);
Data.Username = username;
Data.Password = password;
Data.Language = Context.Language.First(c => c.Id == langId); //Gets the foreign entity
Context.SaveChanges();
return Data;
}
}
This works without exception. But normally this SAVE method is an implemented method by an interface. So, I'd rather to pass an OBJECT value to the SAVE method than passing all the fields like above.
For example I'd like to use this:
//Member is an EntityFramework Object which was created during EF Model when I added by visual studio
//Filter is the method that returns an EntityFramework Object from EF (and eventually database) with given some arguments
//SomeArguments could be MemberId = 2
Member modifiedMember = currentMemberManager.Filter(someArguments);
modifiedMember.UserName = "newvalue";
currentMemberManager.Save(modifiedMember);
In the SAVE method perhaps I could use like this:
public Member Save(Member modifiedMember)
{
using (var Context = base.Entities)
{
var Data = Context.Member.First(c => c.Id == modifiedMember.Id);
Data.Username = modifiedMember.Username;
Data.Password = modifiedMember.Password;
Data.Language = Context.Language.First(c => c.Id == modifiedMember.LanguageId); //Gets the foreign entity
Context.SaveChanges();
return Data;
}
}
If I am gonna use this just like the one right above, I think I should NOT use the passing object EF Object and instead perhaps I should use some other class that I'll write just to map the parameters to the EF Member Entity.
First question: Is this a good approach?
public class MyMember
{
public long Id { set; get; }
public string Username { set; get; }
public string Password { set; get; }
public int LanguageId { set; get; }
}
MyMember.Username = "NewValue";
MyMember.LanguageId = 4; //4 as in new value
currentMemberManager.Save(MyMember); //As you can see MyMember is not an EF Entity in here
But this will take long time since there is already some written code.
Second question: Can't I use an EF Entity object OUTSIDE and modify it there to save it IN the MemberManager class?
Again, thank you very much for your help in advance.
Here is the answer:
How to update entity?
And I thank you for the ones who helped me.

Categories

Resources