I've tried to do this and have been unsuccessful.
Is it possible from within my custom module to have a migration to add a column to an existing Orchard module outside of my module? (For instance, trying to add a column named "UseFluidContainer" to a widget part.
Thanks!
Edit:
I have tried the following in a migration file of the module I've created but the column is not added:
public int Create() {
SchemaBuilder.AlterTable(typeof(WidgetPartRecord).Name, table => table
.AddColumn<bool>("UseFluidLayout", c => c.WithDefault(false)));
return 1;
}
Related
I have a table with id,details.Field 'details' contain single family member details like name,age,dob etc.
table like,
|id|details|
|---|---|
|1|{'name':'john','age':'12','dob':'19-06-1987','address':'address','family_id':"1"}|
I want to delete multiple user details using excel file and data in details column (Id should not given).the excel file contain 3 column name,family_id (unique with each family),dob.i need to check those 3 field match with details column and delete the row.
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithValidation;
class ImportDeleteFile implements ToModel, WithValidation
{
use Importable;
public function rules(): array
{
return [
//what i do here
];
}
I am importing an excel file in Laravel 7 that contains employees' records. The file has Columns Employee No. and date. There are duplicate rows that contain the same employee No. and date. I want to store only 1 such record and discard the duplicate rows. Is there any way to do so?
If you're using laravel-excel, you can define rules for validation and check for duplicate records.
In below example, I've checked for duplication for user with email address.
class ModelImport implements ToModel, WithValidation
{
public function rules(): array
{
return [
'1' => \Illuminate\Validation\Rule::unique('users', 'email'),
];
}
}
Then, add skipping errors by implementing Maatwebsite\Excel\Concerns\SkipsOnFailure interface as stated in here
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithValidation;
class ModelImport implements ToModel, WithValidation, SkipsOnFailure
{
use Importable, SkipsFailures;
}
In the following instance, I have tried to simplify an issue to root components.
I've got a very simple SQL Azure database where I created a test table called Table1. Azure creates an ID field with Is Required, Is Primary Key checked. It will NOT allow to check the box Is Identity. There are a couple of other fields which are simply required.
In my VS2012 Project, I have created an LinqToSql Class which created a ProductionDataClasses1.dbml object.
I simply want to add a record to this table thru the method shown below. From what I am reading, ID would be set during the SubmitChanges() after InsertOnSubmit(NewRecord) is specified.
It does work the first time but value is set to zero. On subsequent save, I get an exception (basically it a duplicate record because ID=0 already exists).
To put this into context, I have included some sample code below. The idea is to first check if the record exists and update. If not, I want to add a record.
My question is... Do I need to manually set ID? If so, how do I set the value to an int and how to a retrieve the next value. I tried changing to a Guid but not allowed.
Here is my code sample:
public bool AddTestRecord(string someValue)
{
ProductionDataClasses1DataContext context = new ProductionDataClasses1DataContext();
try
{
var ExistingRecord = context.Table1s.SingleOrDefault(c => c.TextKey == someValue);
if (ExistingRecord == null)
{
var NewRecord = new Table1();
// NewRecord.ID = ???? ; How Do I Manually Set. It is getting set to 0 causing a duplicate value exception
NewRecord.TextKey = someValue;
NewRecord.AnotherValue = DateTime.Now.ToShortTimeString();
context.Table1s.InsertOnSubmit(NewRecord);
}
else
{
ExistingRecord.AnotherValue = DateTime.Now.TimeOfDay.ToString();
}
context.SubmitChanges();
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
context.SubmitChanges();
}
}
I would suggest manually running a SQL script to alter the table and make the column an identity. Look at this answer
Adding an identity to an existing column
Thanks for your reply.
I just was finally able to make this work on a new table and will try to follow along your instructions to make modifications to my real table. My code (as written above) was OK so the issue is in the SQL Azure table definition.
I found the issue is that when you create a new table in SQL Azure, it creates a table with three fields, ID, Column1, Column2. By default, ID is set as the Primary Key but none are checked as Is Identity.
To make this work, I made ID the Is Identity and unchecked PrimaryKey and Column1 the In Primary Key. Thus when a new record is saved, the ID is set and Column1 is checked to make sure it is not already in the system. I had to do this when the table was first created. Once saved, it would not allow me to change.
Afterwards, I updated my Linq To SQL class and dropped the new table in. I noted that now the AutoGenerated Value on ID and PrimaryKey on Column1 was set and my code worked.
I am using EF5 and code first to create database. I have one string field and would like to put Index(non-unique) on it. Is it possible to create Index using Data Annotations?
EF doesn't have a special way to create indices, you need to revert to an old style SQL statement.
you can use the seed method to do this
protected override void Seed(EntityMappingContext context)
{
context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON TABLE (COLUMN)");
}
as described here: Adding index to a table
If you're using Code Migrations, you could modify a migration ('Up' method) to add an index to a new table:
CreateTable(
"dbo.tablename",
c => {...})
.PrimaryKey(t => t.PrimaryKeyColumn)
.Index(t => t.IndexColumn);
...or, if you're adding an index to an existing table, use:
CreateIndex("dbo.tablename", t => t.IndexColumn)
Don't forget to delete the index when downgrading the db ('Down' method)
DropIndex(...)
I used the subsonic generator to create some aspx pages, It works fine.
On some of the pages it automaticaly generated the dropdown boxes for foreign key values.
How Can change that value in the load drops code? Or where I need to change it.
For instance I have a workers table and a workersweek table.
The workers table has a workerid,firstname and lastname field and the workersweek has a workerID field.
The generator automatically set it up to show the firstname in the dropdown. I want to change the value to be both firstname and lastname.
I am sure I will have to add code that does something like firstname + " " + Lastname.
I am just not sure where to do it withing the code that was generated. I see the load drops sub, but it does not seem like that is the one I need to modify.
If it loads from a foreign key then it loads from a database table.
If you need to concat fields in the query, try making a view with the fields concatenated. e.g. select fName + ' ' + lName as FullName from table
Then in the code behind for the aspx page, select that from the view to load the combobox.
Alternatively try using the 'partial' class functionality to create a new bind-able property. This works for me a treat and has the added bonus of consistent presentation of data through out my apps (With the added bonus of not having to change anything in the database - helpful if you have DBA's from hell that demand 18 levels of change control to get anything done.)
So if your tables class file is "workers.cs" and contains a class called "workers.cs", simply create another class file called "workers_custom.cs" (use your own conventions for working with partial classes) that contains the rest of the partial class, in this case something like:
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using SubSonic;
using SubSonic.Utilities;
namespace YOURCOMPANY.YOURSYSTEM.YOURDAL {
public partial class Workers {
[Bindable(true)]
public string displayWorkersName {
get {
try {
return this.fName + ", " + this.lName;
} catch {
//Your own error handling here
return IsNew ? "##New##" : "##Undefined##";
}
}
}
}
}
(Note you will need to change the bound property member of your control to your new property - in this case: "displayWorksName")