How to read and save this like this? Automatically! (2 Viewers)

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 18:39
Joined
Mar 22, 2009
Messages
998
whenever an ID is read, The object (for example: Customer) has to be returned. Whenever an Object is saved to a table the ID (Property) has to be saved. (Automatically)
Note:
All my Class files must be having an ID property for sure.
 
"...whenever an ID is read..."
Read by what? Read where? Read how?

"The object (for example: Customer) has to be returned"

Returned to what?

"Whenever an Object is saved to a table the ID (Property) has to be saved."

If the ID in question is an AutoNumber, that will happen when the record for that object is saved in the table.

Also, maybe, if you explain the business process involved and the goal you want to achieve, someone could offer useful suggestions.
 
System.InvalidOperationException: 'The 'Customer' property 'Customer_Shipping.Customer' could not be mapped because the database provider does not support this type. Consider converting the property value to a type supported by the database using a value converter. See https://aka.ms/efcore-docs-value-converters for more information.
 
"...whenever an ID is read..."
Read by what? Read where? Read how?

"The object (for example: Customer) has to be returned"

Returned to what?

"Whenever an Object is saved to a table the ID (Property) has to be saved."

If the ID in question is an AutoNumber, that will happen when the record for that object is saved in the table.

Also, maybe, if you explain the business process involved and the goal you want to achieve, someone could offer useful suggestions.
Our Databases doesn't have the capacity to store objects directly right? So whenever the system tries to store an object into a table. I automatically want the sytem to store the Object's ID instead into the table. I also want the Inversion of the same task to be done. As I have the Object itself as the property in my class files. Hope you understand right now... Can be Done? It would be better if the above process is done automatically for all class files instead of coding it again and again for different class files...
 
What objects are you trying to save? What is the data type of the column in the table? Post your code. Post your schema. You can always trap data errors when you are trying to save a record. Is that what you are asking how to do? Why don't you ALWAYS save the ID? Too many questions to provide any rational answer.
 
Sorry. I should have posted this in the .Net section. Anyways. I am developing a MAUI app in Visual Studio where there is a concept called DbContext exists. It acts like a middle ware between the database and the class files (objects). In Database tables we cannot stores Objects directly but only it's Keyfield like ID but in the class files we can have an Object itself as a property. It's seems a bit difficult because I want the keep the ID property when comes to a Table and Object when it comes to Class's property. There are Advantages...
 
Customer.cs
C#:
using System.ComponentModel.DataAnnotations.Schema;
namespace M_VM_V
{
    public class Customer
    {
        private readonly MyDbContext _context;

        public Customer() { }
        public Customer(MyDbContext context)
        {
            _context = context;
        }
        private int _id;
        private string _name;
        private string _name_tamil;
        private string _city;
        [NotMapped]
        private Customer_Shipping _Shipping;
        public int ID
        {
            get
            {
                return _id;
            }   
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public string Name_tamil
        {
            get
            {
                return _name_tamil;
            }
            set
            {
                _name_tamil = value;
            }
        }
        public string? City
        {
            get
            {
                return _city;
            }
            set
            {
               _city = value;
            }
        }
        public Decimal Shipping
        {
            get
            {
                return _context.Shipping
                    .Where(cs => cs.Customer.ID == ID)
                    .OrderByDescending(cs => cs.Moment)
                    .Select(cs => cs.ShippingAmount)
                    .FirstOrDefault();
            }
            set
            {
                _Shipping.Customer = this;
                _Shipping.Moment = DateTime.Now;
                _Shipping.ShippingAmount = value;
            }
        }
    }
}
Customer_Shipping.cs

C#:
namespace M_VM_V
{
    public class Customer_Shipping
    {
        private int _id;
        private Customer _customer;
        private DateTime _moment;
        private Decimal _shipping_Amount;
        public int ID
        {
            get
            {
                return _id;
            }
        }
        public Customer Customer
        {
            get
            {
                return _customer;
            }
            set
            {

            }
        }
        public DateTime Moment
        {
            get
            {
                return _moment;
            }
            set
            {
                _moment = value;
            }
        }
        public Decimal ShippingAmount
        {
            get
            {
                return _shipping_Amount;

            }
            set
            {
                _shipping_Amount = value;
            }
        }
    }
}
 
Sorry. Upon seeing High traffic in this section I posted here. Kindly move it to .Net section if appropriate. Thanks.
 

Users who are viewing this thread

Back
Top Bottom