C# names starting with numbers workaround [duplicate] - c#

I'm mapping up data from an SQL database into an object in c#. The problem is, one of the columns is unfortunately named "100_hrs". So when I am making my C# object, I get an error in the declaration:
public float 100_hrs {get; set;}
I've tried using # in front but it does not work. If the property is not named the same as the table column, then it does not map up. How can I map this up?

You can use column attribute as below
[Column("100_hrs")]
public float hundred _hrs {get; set;}
MSDN
MVC3 naming a column beginning with a number

Answering why the # didn't work.
# lets you declare variables with keywords names, it doesn't allow you to use invalid tokens.
MSDN
Regarding to the main question, it seems like you should change the table column name.
If you're using some sort of mapping engine like NHibernate of EntityFramework, you can change the mapping file. Example:
[Column("100_hrs")]
public float hrs100 {get; set;}

Why not use underscore before the number say _100_hrs since you could easily manipulate it by string extraction if you want to map it to your column? Although there is a little overhead it sure solve your problem.

Related

how can i insert new value to existing record in mongodb?

I want to update my existing record with a new value in MongoDB, here I don't want to remove old value
here you can see that I have punching detail records and I want to add every time machine number and punching time
here is my model
now I want to add new value every time there will be a puch in machine and
can anyone have a solution?
You need to use Array data type for punching detail field and use MongoDB $push operator with update command.
For More Info: https://docs.mongodb.com/manual/reference/operator/update/push/
Hope it helps.
I will recommended to use Different Model for Punching Details and use mongodb ObjectId as a reference of Punching machine model.
You can also create a Array and append new document on each update but it will cause performance issue when your array is too large.
So you can use Punching_detail model as mentioned below
public class punching_detail {
public string punchingModel_id {get;set;}
public int machine_number {get;set;}
public DateTime punching_time {get;set;}
}

How to define the Bit datatype in a method parameter in C#?

I have a SQL table on a database which has a column in it with the Bit datatype. I'm trying to define a method in my C# application which takes two of the columns from the table and uses them as parameters.
Here is the code
public void Edit_NAA_ApplicationsFirm(int ApplicationId, string UniversityOffer, bit Firm)
{
//This line declares a variable in which we store the actual values from NAA_Applications based on the associating ID
NAA_Applications AppFirm = Get_Applicant_Application(ApplicationId);
//Here we tell the application that the values edited are equal to the values within the table
AppFirm.Firm = Firm;
//Any of these changes are then saved.
_context.SaveChanges();
}
The only issue is the the program keeps trying to convert bit to BitConverter. When I change it to bit it has issues accepting it as a datatype.
It should be worth noting I'm building the application in an ASP.Net Framework solution.
Could anyone tell me what it is I'm doing wrong? Am I just referring to the datatype wrong?
Looks like you're using Entity Framework.
It'll use a .NET boolean to represent a T-SQL bit. That would be a sensible way to do it for any other data access method as well. boolean true will convert to 1 in the bit field, and false to 0.
In fact it's even documented, more than once, that this is the correct .NET CLR type to use. See https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/sql-clr-type-mapping , https://msdn.microsoft.com/en-us/library/cc716729(v=vs.100).aspx and probably others.
So in your case bool Firm would be appropriate.
The correct datatype for bit in c# is boolean. so
public void Edit_NAA_ApplicationsFirm(int ApplicationId, string UniversityOffer, bool Firm)
if I understand corectly, try use bool Firm
in c#, value 1 from database is converted to "true", and "true" from code is converted to 1 in database

EF: Is it OK to use default value for the created date property?

I want to set the CreatedDate column to the current datetime whenever a new row is inserted, so I made it like this:
public DateTime CreateDate { get; set; } = DateTime.Now;
I tested it and it works just fine, Is this a practical approach? I saw many articles achieve the same with so much code and using fluent API, no one mentioned this simple method?
That depends on what you want to accomplish.
I saw many articles achieve the same with so much code and using fluent API,
Most of those articles will be about adding default values at the database level. You're not doing that. If you insert a row into your table using plain SQL, and don't specify a value for CreatedDate, you'll get an error.
With what you're doing, CreatedDate always needs to be specified in SQL when inserting. But Entity Framework will always specify it in the SQL when inserting, and completely ignore any default value set at the database level.
So if that's what you want -- the default value only gets applied when creating objects through C# -- then what you're doing is totally fine. It may also be written as setting the value from inside the class's constructor.
#Alex Kozlowski raises a good comment though, which is that DateTime.Now may not be the value you expect to be inserted. It depends on which system is running the code. The time zone may be different from your server's, or the clock may be out of sync.
Yes. This feature was added in c# version 6 onwards. This will work from c#-6 onwards only. And that's why you won't find it in many articles.

Dapper.Rainbow with existing database schema

I am trying out Dapper. I like what I have seen so far. In order to do simple CRUD, I use Dapper.rainbow. It works very well. However, it works only if the table has identity column with name Id. It makes sense to have db like that but I can not ask for column name change in database just to use Dapper. To be more clear, I am working with database like Northwind Db. It has tablename repeated in Id column everywhere.
In order to handle this,I changed the Dapper.Rainbow code as below:
public T Get(TId id,string idColumnName="Id")
{
return database.Query<T>("select * from " + TableName + " where "+idColumnName+" = #id", new { id }).FirstOrDefault();
}
Is there a better way to handle this such as Column mapping /annotations or something completely different?
I have read questions like these
Manually Map column names with class properties
Dapper.Rainbow VS Dapper.Contrib
( I came across similar little problem with Dapper.Contrib, I will ask it separately).
Update - Not sure if the answers are applicable to my Dapper.Rainbow problem here (Atleast, I don't see how).
Thanks for help in advance!
I had a similar problem. The existing Dapper extensions did not fit my ideal pattern. I wanted simple CRUD operations with smart defaults without anything extra. I also wanted to have models with additional properties that did not directly map to the database. For example - a FullName property that combines FirstName and LastName in its getter - and not add FullName to the Insert and Update statements.
I wanted the primary key column to be Id in most cases but allow overriding with an attribute.
Finally, I wanted the table name to match the class name by default but allow overriding with an attribute.
I ended up writing my own extension to Dapper that solves these issues. Its on Github and Nuget. Hopefully it will be helpful to you.
https://github.com/ericdc1/Dapper.SimpleCRUD

subsonic 3 and active record replacing single quotes in data

hi i am using subsonic 3 and activerecord it is my first time with this and i was just wondering if anyone can point me to some reading material with regards to inserting records.
the examples i can find for adding and editing only seem to add and update the data, but i want to check the data first and replace any single quotes with doubles etc etc
or even encode the data first, before it is added or updated, so if anyone can point me in the right direction of some real examples that would be much appreciated
thanks
dave
the column are represented as normal properties in active record objects.
the values for columns are passed using these properties. if you want to do any modifications before before pushing values to database you only need to modify these properties and then call save method on the object. like:
var arObj=new MyOrder();
arObj.OrderId = 15;
arObj.OrderDate = DateTime.Now;
arObj.Description = "..................";
suppose you want to modify Description property of MyOrder object before saving to database:
arObj.Description = Abracadabra(arObj.Description);
arObj.Save();

Categories